当前位置:   article > 正文

SpringBoot项目打包成jar后,使用ClassPathResource获取classpath(resource)下文件失败_jar classpathresource

jar classpathresource

我在读取邮件模板的时候,本地测试使用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。

  1. String txt = "";
  2. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  3. Resource[] resources = resolver.getResources("templates/layout/email.html");
  4. Resource resource = resources[0];
  5. //获得文件流,因为在jar文件中,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
  6. InputStream stream = resource.getInputStream();
  7. StringBuilder buffer = new StringBuilder();
  8. byte[] bytes = new byte[1024];
  9. try {
  10. for (int n; (n = stream.read(bytes)) != -1; ) {
  11. buffer.append(new String(bytes, 0, n));
  12. }
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. txt = buffer.toString();

然后,想知道更多的咱们就继续看看是怎么回事,如果只是为了解决问题,那就可以忽略下面的内容了。

为了老夫好奇的心,我们继续探索下去,到底是怎么回事?我们先看看之前的代码:

  1. String txt = "";
  2. Resource resource = new ClassPathResource("templates/layout/email.html");
  3. 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)中,有如下的判断:

  1. public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
  2. Assert.notNull(resourceUrl, "Resource URL must not be null");
  3. if (!"file".equals(resourceUrl.getProtocol())) {
  4. throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);
  5. } else {
  6. try {
  7. return new File(toURI(resourceUrl).getSchemeSpecificPart());
  8. } catch (URISyntaxException var3) {
  9. return new File(resourceUrl.getFile());
  10. }
  11. }
  12. }

因为resourceUrl.getProtocol()不是file,而是 jar,这样就抛出了一个FileNotFoundException异常。

ResouceUtils.getFile()是专门用来加载非压缩和Jar包文件类型的资源,所以它根本不会去尝试加载Jar中的文件,要想加载Jar中的文件,只要用可以读取jar中文件的方式加载即可,比如 xx.class.getClassLoader().getResouceAsStream()这种以流的形式读取文件的方式,所以使用读取文件流就可以拿到了。

解决方案一:在jar包中使用文件流读取。

  1. ExcelWriter excelWriter = EasyExcel.write(httpResponse.getOutputStream())
  2. .withTemplate(new ClassPathResource("templates/excel/b2cSaleOrder/OrderListExportTemplate.xlsx").getInputStream())
  3. .build();
  4. WriteSheet writeSheetOne = EasyExcel.writerSheet("Sheet1").build();
  5. excelWriter.fill(listOne, writeSheetOne);
  6. excelWriter.finish();

解决方案二:通过指定类所在的目录来指定模板所在根路径

  1. String fontPath = new ClassPathResource("/fonts/", FontUtil.class.getClassLoader()).getFile().getPath();
  2. String templatePath = new ClassPathResource("/templates/excel/b2cSaleOrder/OrderListExportTemplate.xlsx", FontUtil.class.getClassLoader()).getFile().getPath();

参考文章1:SpringBoot项目打包成jar后读取文件的大坑,使用ClassPathResource获取classpath下文件失败 - Posts - 任霏的博客

参考文章2:打包成jar后读取文件的大坑:使用ClassPathResource获取classpath下文件失败_赶路人儿的博客-CSDN博客 

参考文章3:freemaker模板位置ClassTemplateLoader的绝对路径相对路径设置方法_机械手学Java的博客-CSDN博客_freemarker 模板路径

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/815951
推荐阅读
相关标签
  

闽ICP备14008679号