酆都城主

博客园 首页 联系 订阅 管理

通过模板的形式生成excel文件时,在开发时使用

File file = ResourceUtils.getFile("classpath:demo.xlsx");

能够正常读取文件,放到centos服务器上,程序报错

cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/

推荐使用Resource进行文件的读取,解决了问题

1、在类中注入 

@Autowired
private ResourceLoader resourceLoader;

2、然后在代码中使用resource.getInputStream()

Resource resource = resourceLoader.getResource("classpath:demo.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(resource.getInputStream()); 

 

 

参考:https://smarterco.de/java-load-file-from-classpath-in-spring-boot/

posted on 2019-06-05 15:25  酆都城主  阅读(1172)  评论(0编辑  收藏  举报