首页 > 其他 > 详细

项目加载excel模版文件遇到的坑

时间:2019-02-14 15:38:13      阅读:184      评论:0      收藏:0      [点我收藏+]

  最近项目中有个需求:先加载excel模版文件,然后填充数据再下载。模版文件是提前做好的,首先想到是放在服务器的硬盘上,

通过 new File("/xxx/xxxx.xls") 这种方式就可以拿到。但是项目用的是docker ,每次服务重启构建都要重新上传,太麻烦。所以还是放到项目里面比较方便,项目用的springboot,静态文件放到resource目录下面。

 

技术分享图片

通过这种方式拿到文件: File file = new File(this.getClass().getClassLoader().getResource("template/export.xls").getFile());

本地测试ok ,但是到了服务上 报错:file not found。

什么情况?方式不对?ok 我换一种方式读取

  ClassPathResource classPathResource = new ClassPathResource("template/export.xls");

  File file =  classPathResource.getFile();

本地测试ok,服务器还是报错:file not found。

经研究发现,服务器上是jar包,访问里面到静态文件必须要通过流到方式读取文件。

所以要用这种方式读取文件:

  ClassPathResource classPathResource = new ClassPathResource("template/export.xls");
  InputStream initialStream = classPathResource.getInputStream();

 

项目加载excel模版文件遇到的坑

原文:https://www.cnblogs.com/yjw-James/p/10373804.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!