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

问题场景

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

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

问题环境

软件版本
springboot2.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)));

结果

顺利解决问题!!!

总结

学无止境!

随缘求赞

如果我的文章对大家产生了帮忙,可以在文章底部点个赞或者收藏;
如果有好的讨论,可以留言;
如果想继续查看我以后的文章,可以点击关注
可以扫描以下二维码,关注我的公众号:枫夜之求索阁,查看我最新的分享!
在这里插入图片描述
拜拜

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫夜求索阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值