返回

问题解决系列:cannot be resolved to absolute file path because it does not reside in the file system_ jar

发布时间:2022-12-22 03:44:26 383
# java# java# 软件# 扫描# 软件

问题场景

springboot打包之后,读取资源文件内容,报错,报错提示如下:

cannot be resolved to absolute file path because it does not reside in the file system: jar

问题环境

软件

版本

springboot

2.1.1.RELEASE

问题原因

打包之后,spring没办法通过File的形式访问jar包里面的文件。

解决方案

有两种方案:

1. 使用resource.getInputStream()读取文件内容

2. 缓存到临时文件,使用临时文件路径进行读取

样例代码如下:

String tempPath = System.getProperty("java.io.tmpdir")+"tomcat_"+System.currentTimeMillis();
String tempFile = tempPath+File.separator+fileName;
Resource resource = new DefaultResourceLoader().getResource("classpath:META-INF/resources"+filePath);
if (resource == null) {
return "";
}

File file = new File(tempPath);
if (!file.exists()) {
file.mkdir();
}
IOUtil.cp(resource.getInputStream(),new FileOutputStream(new File(tempFile)));

结果

顺利解决问题!!!

总结

学无止境!

随缘求赞

如果我的文章对大家产生了帮忙,可以在文章底部点个赞或者收藏;

如果有好的讨论,可以留言;

如果想继续查看我以后的文章,可以点击关注

可以扫描以下二维码,关注我的公众号:枫夜之求索阁,查看我最新的分享!

问题解决系列:cannot be resolved to absolute file path because it does not reside in the file system_ jar_spring

 

问题解决系列:cannot be resolved to absolute file path because it does not reside in the file system_ jar_System_02

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
JAVA-枚举使用 2022-12-22 03:19:49