读取resources下的文件quotaShow.jasper
本地开发环境能正常下载:
ClassPathResource resource = new ClassPathResource("jasper" + File.separator + "quotaShow.jasper");
reportFile = resource.getFile();
打jar包发布至linux服务器时报错:
class path resource [xxxx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:xxxx.jar!/BOOT-INF/classes!xxxx
解决方法:
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("jasper"+File.separator+"quotaShow.jasper");
Resource resource = resources[0];
InputStream is = resource.getInputStream();
String filePath= this.getClass().getClassLoader().getResource("jasper"+File.separator+"quotaShow.jasper").getFile();
File reportFile = new File(filePath);
FileUtils.copyInputStreamToFile(is,reportFile);
拿到file后,后面走正常的下载流程。