SpringBoot项目打包成jar后读取文件的大坑,使用ClassPathResource获取classpath下文件失败
我在读取邮件模板的时候,本地测试使用ClassPathResource都可以正常读取,但打包成jar包传到服务器上就无法获取了,报错信息是: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,话不多说,先看正确的获取方法:使用PathMatchingResourcePatternResolver。
String txt = "";
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("templates/layout/email.html");
Resource resource = resources[0];
//获得文件流,因为在jar文件中,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
InputStream stream = resource.getInputStream();
StringBuilder buffer = new StringBuilder();
byte[] bytes = new byte[1024];
try {
for (int n; (n = stream.read(bytes)) != -1; ) {
buffer.append(new String(bytes, 0, n));
}
} catch (IOException e) {
e.printStackTrace();
}
txt = buffer.toString();
然后,想知道更多的咱们就继续看看是怎么回事,如果只是为了解决问题,那就可以忽略下面的内容了。
为了老夫好奇的心,我们继续探索下去,到底是怎么回事?我们先看看之前的代码:
String txt = "";
Resource resource = new ClassPathResource("templates/layout/email.html");
txt = fileUtil.readfile(resource.getFile().getPath());
其实这是一个jar包发布的大坑,相信很多小伙伴遇到了读取文件的问题,其实使用getFile()的时候的坑,为了弄明白到底是咋回事,我进行了跟踪,结果返回的是一个Jar协议地址:jar:file:/xxx/xx.jar!/xxxx。
然后继续跟踪到org.springframework.util.ResourceUtils#getFile(java.net.URL, java.lang.String)中,有如下的判断:
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
Assert.notNull(resourceUrl, "Resource URL must not be null");
if (!"file".equals(resourceUrl.getProtocol())) {
throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);
} else {
try {
return new File(toURI(resourceUrl).getSchemeSpecificPart());
} catch (URISyntaxException var3) {
return new File(resourceUrl.getFile());
}
}
}
因为resourceUrl.getProtocol()不是file,而是 jar,这样就抛出了一个FileNotFoundException异常。
ResouceUtils.getFile()是专门用来加载非压缩和Jar包文件类型的资源,所以它根本不会去尝试加载Jar中的文件,要想加载Jar中的文件,只要用可以读取jar中文件的方式加载即可,比如 xx.class.getClassLoader().getResouceAsStream()这种以流的形式读取文件的方式,所以使用读取文件流就可以拿到了。
商业用途请联系作者获得授权。
版权声明:本文为博主「任霏」原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接及本声明。
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。- 2020-09-12 22:49:35
- Wuxi, Jiangsu, China
- 回复
微信订阅号
扫码关注「任霏博客」微信订阅号- 大佬 引入jar包那里的 driver class 怎么选的?
- 我也遇到了这个问题,已经解决了,在此分享一下 1、宿主机也要创建kingbase的用户和用户组,并且要查看一下用户和用户组的ID(这个很重要) 2、把data目录的用户和用户组设置为kingbase 3、先不要把data路径挂载到宿主机上,这时就可以正常启动,启动后进入容器,查看一下容器内的kingbase的用户和用户组ID是多少,和第一步的ID是否一致,如果ID一致,那正常挂载目录就行;如果ID不一致,那就需要修改Dockerfile文件,在构建镜像时,修改容器内的用户和用户组ID,必须和宿主机的保持一致。然后重新构建镜像,就可以正常挂载宿主机目录了 4、其实直接修改宿主机的用户和用户组ID也是可以的,但是容器内的ID一般是1000,但是宿主机的这个ID很可能已经被占用了,无法修改,就只能修改容器内的ID
- 接口已经允许跨域请求,也就是说你可以在你的页面上调用,获取用户的公网 IP。 如果你还需要其他需求,可以提交 Issue 给我。
- V008R003C002B0320 这个对应的jdbc链接驱动你在哪里找到的?我也遇到了这个问题。
- WARNING: max_connections should be less than orequal than 10 (restricted by license) HINT: the value of max_connect is set 10 WARNING: max_connections should be less than orequal than 10 (restricted by license) HINT: the value of max_connect is set 10 kingbase: superuser_reserved_connections must be less than max_connections 我按照文档修改了以后,不知道如何重启。
- 然后把数字都改成 1 再启动。 如何重新启动?
- ksql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.KINGBASE.54321"
- 进入容器查看一下日志,是不是启动失败了,日志文件在:/opt/kingbase/logfile
- ksql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.KINGBASE.54321"?
- 先通过 docker exec -it 容器名/id /bin/bash 进入容器,然后在容器中使用 ksql 客户端进行连接数据库:/opt/kingbase/Server/bin/ksql -U system test
- 免费.ml域名10年委托合同到期被马里共和国收回域名经营权
- 从极狐Gitlab看各种中间件技术选型
- 时隔十年首次收到 Google AdSense 的付款
- ga域名被加蓬共和国从Freenom公司手中收回域名经营权
- Freenom 被 Meta(Facebook) 起诉导致暂停 .tk/.ga/.ml/.cf/.gq 等新域名注册
- 生花妙笔信手来 – 基于 Amazon SageMaker 使用 Grounded-SAM 加速电商广告素材生成 [1]
- github.renfei.net 不再完整代理 Github 页面改为代理指定文件
- 优雅的源代码管理(三):本地优雅的使用 Git Rebase 变基
- 优雅的源代码管理(二):Git 的工作原理
- 优雅的源代码管理(一):版本控制系统 VCS(Version Control System)与软件配置管理 SCM(Software Configuration Management)