jar:file:/xxx/xx.jar!/BOOT-INF/classes!/xxxx

问题描述

 Java application 经过docker build之后,得到app.jar。

使用绝对路径访问文件001.jpg时会报错。

{
  "code": "500",
  "message": "class path resource [001.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app/app.jar!/BOOT-INF/classes!/001.jpg",
  "data": "class path resource [001.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app/app.jar!/BOOT-INF/classes!/001.jpg"
}

原因分析

在jar文件中,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流。

解决方案

public class BaiduOCRController {
...
InputStream resourceAsStream = BaiduOCRController.class.getResourceAsStream("/001.jpg");
FastByteArrayOutputStream read = IoUtil.read(resourceAsStream);
byte[] data = read.toByteArray();
...
}

BaiduOCRController 与 001.jpg在相同目录。

Class.class.getResourceAsStream 的默认路径

默认情况下,资源路径是相对于该类所在的包路径。

com.example.MyClass

 MyClass.class.getResourceAsStream("data.txt"),该方法会在与MyClass类相同的包路径下查找名为data.txt的资源文件。

MyClass.class.getResourceAsStream("/data.txt")将会在类路径的根目录下查找名为data.txt的资源文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值