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

朴灿烈づ我的快乐病毒、 2024-03-30 11:08 177阅读 0赞

https://cloud.tencent.com/developer/article/2193203

项目场景:

在Springboot中利用Resource来获取文件并在前端返回该文件, 本地测试正常, 打包到远程报错: cannot be resolved to absolute file path because it does not reside in the file system


问题描述:

紧接上一个问题: 项目打包成 jar 后包无法读取src/main/resources下文件, 在Springboot打包之后, 无法读取到jar包内的文件, 因此采取Resource来获取jar内相对路径地址的文件. 只有一个需要下载文件的时候没有问题, 然后在指定文件夹下新增一个文件后本地下载正常, 打包后下载出现问题: 下载该文件时, 后端抛出异常, 异常如下

  1. class path resource [static/xxx模板.xlsx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/mis-project-java-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/%e5%b7%a5%e8%b5%84%e8%a1%a8%e6%a8%a1%e6%9d%bf.xlsx

复制


原因分析:

需要下载文件存放的地址:

19f6b1482f5b5baa92f2f4b18ebef07d.png

修改前代码:

  1. @Override
  2. @SneakyThrows(IOException.class)
  3. public HttpServletResponse downloadFile(HttpServletRequest request, HttpServletResponse response) {
  4. //设置文件路径
  5. org.springframework.core.io.Resource resource = new ClassPathResource("static/" + "xxx模板.xlsx");
  6. if (resource.exists()) {
  7. //异常在下一行抛出
  8. File file = resource.getFile();
  9. response.setCharacterEncoding("UTF-8");
  10. response.setContentType("application/vnd.ms-excel");
  11. String fileName = resource.getURI().toString().substring(resource.getURI().toString().lastIndexOf("/") + 1);
  12. response.setHeader("content-disposition", "attachment;filename=" + fileName);
  13. response.flushBuffer();
  14. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
  15. OutputStream os = response.getOutputStream();
  16. byte[] buffer = new byte[1024];
  17. int i = bis.read(buffer);
  18. while (i != -1) {
  19. os.write(buffer, 0, i);
  20. i = bis.read(buffer);
  21. }
  22. if (bis != null) {
  23. bis.close();
  24. }
  25. return response;
  26. }
  27. return null;
  28. }

复制

对问题部分本地断点调试

本地断点调试:

  1. 可以看到, 调用的是 AbstractFileResolvingResource下的getFIle 方法. 然后该方法会调用 ResourceUtils下的getFile() 方法

    ad4630e5087ca326e76a2f196269cc70.png

  2. 可以看到在ResourceUtils下的getFile() 方法中, 因为 resourceUrl.getProtocol() 为File, 说明该url 地址对应的资源是文件, 所以直接返回这个文件

    f04dd4936c83bfc02926161689fd6407.png

  3. 后续源码如何返回这个文件可以自己继续深入

对问题部分远程断点调试:

  1. 依旧是进入 resource.getFile(); 的方法内. 因为当前 url的protocol 属性时 jar, 不是vfs, 依旧走的是ResourceUtils下的getFile() 方法

  2. 在ResourceUtils下的getFile() 方法中, 因为 resourceUrl.getProtocol() 为jar, 因此会抛出异常

    1ea5455065000923c58625e205aff72f.png

  3. 异常抛出后被全局捕获, 然后在前端显示

769dd28923d2d53aff8c089aa82712a6.png


解决方案:

通过上面的调试我们可以看到, org.springframework.core.io.Resource 这个类的getFile()方法, 会自动获取构建resource对象带参构造中的url, 然后根据这个url确定该文件的类型. 因为在本地时调试时, 通过resource.getFile()获取的url类型的 protocol 属性为File, 所以可以自动生成文件; 然而在将项目打包成jar部署在服务器上时, 因为该文件是在jar里面的. 因此通过resource.getFile()获取的url类型的 protocol 属性为jar. 所以抛出该异常 cannot be resolved to absolute file path because it does not reside in the file system: 文件url.

因此, 方法有两种: 一种是直接将该文件放入服务器其他目录下而不是在jar包中. 另一种就是通过流来获取jar里面的文件. 而本人采取第二种方式, 通过输入流来读取jar内的文件, 然后通过输出流将其输出.


修改后的代码

  1. @Override
  2. @SneakyThrows(IOException.class)
  3. public HttpServletResponse downloadFile(HttpServletRequest request, HttpServletResponse response) {
  4. //设置文件路径
  5. org.springframework.core.io.Resource resource = new ClassPathResource("static/" + "xxx模板.xlsx");
  6. if (resource.exists()) {
  7. response.setCharacterEncoding("UTF-8");
  8. response.setContentType("application/vnd.ms-excel");
  9. String fileName = resource.getURI().toString().substring(resource.getURI().toString().lastIndexOf("/") + 1);
  10. response.setHeader("content-disposition", "attachment;filename=" + fileName);
  11. response.flushBuffer();
  12. //放弃使用直接获取文件的方式, 改成使用流的方式
  13. BufferedInputStream bis = new BufferedInputStream(resource.getInputStream());
  14. OutputStream os = response.getOutputStream();
  15. byte[] buffer = new byte[1024];
  16. int i = bis.read(buffer);
  17. while (i != -1) {
  18. os.write(buffer, 0, i);
  19. i = bis.read(buffer);
  20. }
  21. if (bis != null) {
  22. bis.close();
  23. }
  24. return response;
  25. }
  26. return null;
  27. }

发表评论

表情:
评论列表 (有 0 条评论,177人围观)

还没有评论,来说两句吧...

相关阅读