SpringBoot读取Resource下文件的几种方式读取jar里的excel,文件损坏

在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传;

这里待下载模板位置为resource/template/员工基础信息导入模板.xlsx,

分别尝试了四种读取方式,并且测试了四种读取方式分别的windows开发环境下(IDE中)读取和生产环境(linux下springboot 的 jar包运行读取)。

文件后缀也正确 ,就是打不开

第一种:

# 注意是 Springboot 下的 ClassPathResource , 而不是hutool 下的,否则报错,下载下来excel 后打不开
复制代码
# 注意是 Springboot 下的 ClassPathResource , 而不是hutool 下的,否则报错,下载下来excel 后打不开 
ClassPathResource classPathResource = new ClassPathResource("template/员工基础信息导入模板.xlsx");
InputStream inputStream =classPathResource.getInputStream();

第二种:

复制代码
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/员工基础信息导入模板.xlsx");

第三种:

复制代码
InputStream inputStream = this.getClass().getResourceAsStream("template/员工基础信息导入模板.xlsx");

第四种:

复制代码
File file = ResourceUtils.getFile("classpath:template/员工基础信息导入模板.xlsx");
InputStream inputStream = new FileInputStream(file);
经测试:

前三种方法在开发环境(IDE中)和生产环境(linux部署成jar包)都可以读取到;

第四种只有开发环境 时可以读取到,生产环境读取失败。

推测

主要原因是springboot内置tomcat,打包后是一个jar包,Resource下的文件是存在于jar这个文件里面,在磁盘上是没有真实路径 存在的,它其实是位于jar内部的一个虚拟路径。所以通过ResourceUtils.getFile或者this.getClass().getResource("")方法无法正确获取文件。

只能通过类加载器读取。

前三种都可以读取到其实殊途同归,直接查看底层代码都是通过类加载器读取文件流,类加载器可以读取jar包中的编译后的class文件,当然也是可以读取jar包中的excle模板了。

用解压软件打开jar包查看结果如下:

其中cst文件中是编译后class文件存放位置,excleTemplate是模板存放位置,类加载器读取的是cst下class文件,同样可以读取excleTemplate下的模板的文件流了。

总结一下:

假如文件是在jar包中,读取方式应当使用基于类加载器读取文件流的方式,比如前三种方法;使用基于java中File方式的读取,在jar包情况下是读取不到的,比方说第四种。

复制代码
    /**
     *  下载模板
     *
     * @param fileName          文件名称
     * @param response
     * @return
     * @author wangl
     * @date 2023-10-23
     */
    public static void downloadExcelTemplate(String fileName, HttpServletResponse response) {
        //方法一:直接下载路径下的文件模板(这种方式貌似在SpringCloud和Springboot中,打包成JAR包时,可能会无法读取到指定路径下面的文件)
        try {
            //设置要下载的文件的名称
            resetResponse(fileName, response);
            //通知客服文件的MIME类型
//            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            // 获取文件的路径
            // 注意是 Springboot 下的 ClassPathResource , 而不是hutool 下的,否则报错,下载下来excel 后打不开 
            ClassPathResource classPathResource = new ClassPathResource("/template/" + fileName);
            InputStream inputStream = classPathResource.getInputStream();
            OutputStream out = response.getOutputStream();
            byte[] b = new byte[2048];
            int len;
            while ((len = inputStream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            //修正 Excel在"xxx.xlsx"中发现不可读取的内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"
            response.setHeader("Content-Length", String.valueOf(inputStream.readAllBytes().length));
            inputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
相关推荐
Code季风5 分钟前
Spring Bean的生命周期详解
java·spring boot·spring
万能小锦鲤32 分钟前
《Java EE与中间件》实验三 基于Spring Boot框架的购物车
java·spring boot·mysql·实验报告·购物车·文档资源·java ee与中间件
麦兜*44 分钟前
【Spring Boot】Spring Boot 4.0 的颠覆性AI特性全景解析,结合智能编码实战案例、底层架构革新及Prompt工程手册
java·人工智能·spring boot·后端·spring·架构
全栈凯哥1 小时前
20.缓存问题与解决方案详解教程
java·spring boot·redis·后端·缓存
源码云商2 小时前
基于 SpringBoot + Vue 的 IT 技术交流和分享平台的设计与实现
vue.js·spring boot·后端
ladymorgana9 小时前
【spring boot】三种日志系统对比:ELK、Loki+Grafana、Docker API
spring boot·elk·grafana
程序员秘密基地10 小时前
基于html,css,vue,vscode,idea,,java,springboot,mysql数据库,在线旅游,景点管理系统
java·spring boot·mysql·spring·web3
xdscode13 小时前
SpringBoot ThreadLocal 全局动态变量设置
java·spring boot·threadlocal
天河归来15 小时前
springboot框架redis开启管道批量写入数据
java·spring boot·redis
合作小小程序员小小店15 小时前
web网页,在线%食谱推荐系统%分析系统demo,基于vscode,uniapp,vue,java,jdk,springboot,mysql数据库
vue.js·spring boot·vscode·spring·uni-app