读取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后,后面走正常的下载流程。
如果觉得《springboot打成jar后获取resources下文件失败 cannot be resolved to absolute file path because it does not resid》对你有帮助,请点赞、收藏,并留下你的观点哦!