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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2022-01-12
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2021-11-10
相关资源
相似解决方案