加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

问题解决:CANNOTBERESOLVEDTOABSOLUTEFILEPATHBECAUSEITDOESNOTRESIDEINTHEFILESYSTEM

(2023-06-23 16:07:04)
分类: springboot
转:https://www.freesion.com/article/57841570802/

问题场景

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


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

  • 1

打包之后,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)));

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

结果

顺利解决问题!!!


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有