spring boot 读取resource下的文件


通过模板的形式生成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/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM