当前位置:   article > 正文

Excel模板文件放在项目中部署,下载出来是空文件,没有模板内容_postman 能下载excel 浏览器下载为空

postman 能下载excel 浏览器下载为空

前段时间做项目时,有个需要下载excel模板的需求,但是由于用的是K8S部署,不能直接把模板文件放到指定的服务器,于是就把模板文件放到了项目resources目录下,然后本地测试一切正常,顺利提到了测试。

结果测试环境除了问题,下载下来是个空的excel,返回的response数据也是空。于是开始找问题,本地postman测试,response返回是乱码(其实文件是正常的,应该该是编码问题),文件下载出来没问题。postman连接测试接口,response是空,下载的文件还是空excel。然后网上各种找解决方案,发现百度出来的帖子,基本上千篇一律,就是一个帖子被N多人CV的,连格式都一模一样,找小伙伴帮忙看,也没有找到解决方案,问题就这么搁置了两天,一直没解决。

后来偶尔看到一个帖子,用了不一样的方法读取,我就试了一下,问题就解决了。直接贴代码,注意resource别导错包。

  1. import org.springframework.core.io.Resource;
  2.  
  3. /**
  4.      * 模板下载
  5.      */
  6.     public void downloadTemplate(HttpServletResponse response){
  7.         OutputStream out = null;
  8.         InputStream in = null;
  9.         ByteArrayOutputStream bos = null;
  10.         String fileName = "导入模版";
  11.         try {
  12.             // 读取模板
  13.             Resource res = new ClassPathResource("Template.xlsx");
  14.             XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream());
  15.             // 转换为字节流
  16.             bos = new ByteArrayOutputStream();
  17.             workbook.write(bos);
  18.             byte[] barray = bos.toByteArray();
  19.             in = new ByteArrayInputStream(barray);
  20.             response.reset();
  21.             response.setContentType("application/octet-stream");
  22.             response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
  23.             out = response.getOutputStream();
  24.             byte[] b = new byte[1024];
  25.             int len;
  26.             while ((len = in.read(b)) > 0) {
  27.                 out.write(b, 0, len);
  28.             }
  29.             out.flush();
  30.         } catch (Exception e) {
  31.             log.error("下载模板失败",e);
  32.         } finally {
  33.             if (null != in) {
  34.                 try {
  35.                     in.close();
  36.                 } catch (IOException e) {
  37.                     log.error("关闭资源异常",e);
  38.                 }
  39.                 in = null;
  40.             }
  41.             if (null != out) {
  42.                 try {
  43.                     out.close();
  44.                 } catch (IOException e) {
  45.                     log.error("关闭资源异常",e);
  46.                 }
  47.                 out = null;
  48.             }
  49.             if (null != bos) {
  50.                 try {
  51.                     bos.flush();
  52.                     bos.close();
  53.                 } catch (IOException e) {
  54.                     log.error("关闭资源异常",e);
  55.                 }
  56.                 out = null;
  57.             }
  58.         }
  59.     }

困扰我两天的问题,Resource res = new ClassPathResource("whiteListTemplate.xlsx"); 这一行代码就解决了。

总结:本地测试时,可以直接读取到项目中的文件,所以能把文件下载下来。但是线上部署用的Docker和K8S,是jar包运行的,在读取文件的时候,因为之前的方法问题,一直读取不到文件内容,然后用Resource res = new ClassPathResource("Template.xlsx"); 是通过流的形式读取的,resource继承了InputStreamSource,所以才能以流的方式读取到文件内容。

因为对K8S、Docker和Spring源码并不了解,以上只是我个人的猜想,希望知道原理或者正确答案的小伙伴不吝赐教,万分感谢!

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号