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