spring boot 下载excel文件提示“文件中部分内容有问题。是否让我们尽量尝试恢复

项目场景:

Springboot项目通过IO流写出excel模板文件, 浏览器下载文件并在office 2016 打开后. 出现 文件中部分内容有问题。是否让我们尽量尝试恢复? 如果您信任此工作簿的源, 请单机"是"

在这里插入图片描述


问题描述

通过此代码利用缓冲流读取指定文件后, 然后用输出流返回到响应体中

    @Override
    @SneakyThrows(IOException.class)
    public RpcServiceResult downloadFile(HttpServletResponse response) {
    
    
        //设置文件路径
        org.springframework.core.io.Resource resource = new ClassPathResource("static/" + "模板.xlsx");
        if (resource.exists()) {
    
    
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            String fileName = resource.getURI().toString().substring(resource.getURI().toString().lastIndexOf("/") + 1);
            response.setHeader("content-disposition", "attachment;filename=" + fileName);
            response.flushBuffer();
            BufferedInputStream bis = new BufferedInputStream(resource.getInputStream());
            OutputStream os = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int i = bis.read(buffer);
            while (i != -1) {
    
    
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
            if (bis != null) {
    
    
                bis.close();
            }
            return RpcServiceResult.getSuccessResult("下载成功");
        }
        return RpcServiceResult.getFailureResult(ErrorCodeEnum.RESULT_SYSTEM_ERROR);
    }

原因分析:

一般有3种情况:

  1. 由于没有找到文件,下载的文件字节大小为0,这种情况文件完全打不开
  2. 项目打包进了文件, 但是在打包的过程中.xlsx的文件被压缩
  3. 读取的文件大小和元素文件的大小不一致,这种情况会提升自动修复(本人 office2016遇到的问题)

解决方案:

  • 问题一方案: Resource下的文件是存在于jar这个文件里面,在磁盘上是没有真实路径存在的,它其实是位于jar内部的一个路径. 所以通过ResourceUtils.getFile或者this.getClass().getResource(“”)方法无法正确获取文件. 需要通过 getClass().getClassLoader().getResourceAsStream("static/" + "模板.xlsx");或者 org.springframework.core.io.Resource resource = new ClassPathResource("static/" + "工资表模板.xlsx"); 来获取文件

  • 问题二方案: 项目打包后, 利用解压工具找到该文件(该步骤也可以验证问题一. 如果没有打包进去可以通过在pom中指定某种类型资源文件可被打入来解决). 然后用 office 2016 打开来验证是否报错, 如果提示 文件中部分内容有问题...... 说明打包的过程中被压缩了. 大概率是将文件的写出方法放到了设计响应头方法之外. 从而出现响应头设置失败的问题. 需要将设置响应头的相关方法提到文件写出方法前 outputStream.write(results)
    在这里插入图片描述

  • 问题三解决方案
    网上最多的解决方案是主动在response的Header中设置Content-Length大小, 但这种方式其实是错误的.
    文件的Content-Length其实可以从返回流中直接获取,并不需要用户主动去设置.

    这里的问题核心应该是思考:为什么下载的文件和元素文件的大小会不一致?
    下面2个获取inputStream的长度的API,只有在读取磁盘上具体文件中才比较适用.如果是jar包中的文件,是获取不到大小的

    //加上设置大小
    response.addHeader("Content-Length",String.valueOf(file.length()));
    response.addHeader("Content-Length",String.valueOf(inputStream.available()));
    

    并且由于下载的文件体积总是比实际文件体积大一点点,从而导致文件再被office打开时提示异常修复

    outputStream = response.getOutputStream();
        bis = new BufferedInputStream(inputStream);
         //缓冲流,每次读取1024
        byte[] buff = new byte[1024];
        int readLength = 0;
         while (( readLength = bis.read(buff)) != -1) {
          
          
             //每次写入缓冲流buff读到的字节长度,而不是buff.length
             outputStream.write(buff, 0, readLength);
         }
        outputStream.flush();
    

    出现该问题的原因就是buff.length,数组声明后长度就是固定的,而不是获取里面读取的内容的字节长度,所以导致这里的buff.length的值始终是1024. 因此这里使用spring的FileCopyUtils工具类将数据输出成字节数据, 然后写出, 从而解决该问题. 相关代码如下:

    /**
     * 下载模板
     * @param response
     */
    public void download(HttpServletResponse response) {
          
          
        InputStream inputStream = null;
        BufferedInputStream bis = null;
        OutputStream outputStream = null;
        try {
          
               
            inputStream=getClass().getClassLoader().getResourceAsStream("template/template.xlsx");
    
            response.setContentType("application/octet-stream");
            response.setHeader("content-type", "application/octet-stream");
            //待下载文件名
            String fileName = URLEncoder.encode("模板.xlsx","utf8");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            outputStream = response.getOutputStream();
            //加上设置大小 下载下来的excel文件才不会在打开前提示修复
            //这里流的长度很难在开始读取前获取,特别是打成jar包后,读取inputStream长度经常失败
            //response.addHeader("Content-Length",String.valueOf(classPathResource.getFile().length()));
           //response.addHeader("Content-Length",String.valueOf(inputStream.available()));
             //方式一:经典的缓冲流BufferedInputStream读取法
             //   bis = new BufferedInputStream(inputStream);
                 //缓冲流,每次读取1024
            //    byte[] buff = new byte[1024];
            //    int readLength = 0;
            //     while (( readLength = bis.read(buff)) != -1) {
          
          
            //         outputStream.write(buff, 0, readLength);
            //     }
            //     outputStream.flush();
    
             //方式二:利用spring的FileCopyUtils工具类
             if(inputStream!=null){
          
          
                byte[] results = FileCopyUtils.copyToByteArray(inputStream);
                outputStream.write(results);
                outputStream.flush();
            }
        } catch ( IOException e ) {
          
          
            log.error("文件下载失败,e");
        } finally {
          
          
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(bis);
        }
    }
    

ps: 还有人说可以利用poi的workbook进行读写来解决问题三, 但是我尝试了下, 还是没有解决. 简单看了下原因, 是因为还是用到了缓冲流, 导致写出的文件大小大于实际大小. 在打开文件时依旧会报错.


参考
spring boot中Excel文件下载踩坑大全

猜你喜欢

转载自blog.csdn.net/qq_43371556/article/details/128904267