失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > springboot打成jar后获取resources下文件失败 cannot be resolved to absolute file path because it does not resid

springboot打成jar后获取resources下文件失败 cannot be resolved to absolute file path because it does not resid

时间:2019-05-19 19:39:00

相关推荐

springboot打成jar后获取resources下文件失败  cannot be resolved to absolute file path because it does not resid

读取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》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。