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