最近遇到一个需求,需要在请求接口时,将指定文件夹下的文件进行压缩,然后下载。最初的时候,我使用的是下面的流程进行开发的:

#mermaid-svg-Z7n8tzirEQq2ajKG {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG .error-icon{fill:#552222;}#mermaid-svg-Z7n8tzirEQq2ajKG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Z7n8tzirEQq2ajKG .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Z7n8tzirEQq2ajKG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Z7n8tzirEQq2ajKG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Z7n8tzirEQq2ajKG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Z7n8tzirEQq2ajKG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Z7n8tzirEQq2ajKG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Z7n8tzirEQq2ajKG .marker.cross{stroke:#333333;}#mermaid-svg-Z7n8tzirEQq2ajKG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Z7n8tzirEQq2ajKG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG .cluster-label text{fill:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG .cluster-label span{color:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG .label text,#mermaid-svg-Z7n8tzirEQq2ajKG span{fill:#333;color:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG .node rect,#mermaid-svg-Z7n8tzirEQq2ajKG .node circle,#mermaid-svg-Z7n8tzirEQq2ajKG .node ellipse,#mermaid-svg-Z7n8tzirEQq2ajKG .node polygon,#mermaid-svg-Z7n8tzirEQq2ajKG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Z7n8tzirEQq2ajKG .node .label{text-align:center;}#mermaid-svg-Z7n8tzirEQq2ajKG .node.clickable{cursor:pointer;}#mermaid-svg-Z7n8tzirEQq2ajKG .arrowheadPath{fill:#333333;}#mermaid-svg-Z7n8tzirEQq2ajKG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Z7n8tzirEQq2ajKG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Z7n8tzirEQq2ajKG .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Z7n8tzirEQq2ajKG .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Z7n8tzirEQq2ajKG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Z7n8tzirEQq2ajKG .cluster text{fill:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG .cluster span{color:#333;}#mermaid-svg-Z7n8tzirEQq2ajKG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Z7n8tzirEQq2ajKG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

创建压缩文件
遍历文件
文件流写入压缩文件
写入完毕
将压缩文件写入下载流
删除压缩文件

但是,这样的逻辑没有什么问题,但是下载时经常出现压缩包已损坏的问题。尝试了各种方法,一直没有解决。

最后换了一种思路,如下图:

#mermaid-svg-scGJmW4TDWqez1hf {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-scGJmW4TDWqez1hf .error-icon{fill:#552222;}#mermaid-svg-scGJmW4TDWqez1hf .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-scGJmW4TDWqez1hf .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-scGJmW4TDWqez1hf .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-scGJmW4TDWqez1hf .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-scGJmW4TDWqez1hf .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-scGJmW4TDWqez1hf .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-scGJmW4TDWqez1hf .marker{fill:#333333;stroke:#333333;}#mermaid-svg-scGJmW4TDWqez1hf .marker.cross{stroke:#333333;}#mermaid-svg-scGJmW4TDWqez1hf svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-scGJmW4TDWqez1hf .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-scGJmW4TDWqez1hf .cluster-label text{fill:#333;}#mermaid-svg-scGJmW4TDWqez1hf .cluster-label span{color:#333;}#mermaid-svg-scGJmW4TDWqez1hf .label text,#mermaid-svg-scGJmW4TDWqez1hf span{fill:#333;color:#333;}#mermaid-svg-scGJmW4TDWqez1hf .node rect,#mermaid-svg-scGJmW4TDWqez1hf .node circle,#mermaid-svg-scGJmW4TDWqez1hf .node ellipse,#mermaid-svg-scGJmW4TDWqez1hf .node polygon,#mermaid-svg-scGJmW4TDWqez1hf .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-scGJmW4TDWqez1hf .node .label{text-align:center;}#mermaid-svg-scGJmW4TDWqez1hf .node.clickable{cursor:pointer;}#mermaid-svg-scGJmW4TDWqez1hf .arrowheadPath{fill:#333333;}#mermaid-svg-scGJmW4TDWqez1hf .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-scGJmW4TDWqez1hf .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-scGJmW4TDWqez1hf .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-scGJmW4TDWqez1hf .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-scGJmW4TDWqez1hf .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-scGJmW4TDWqez1hf .cluster text{fill:#333;}#mermaid-svg-scGJmW4TDWqez1hf .cluster span{color:#333;}#mermaid-svg-scGJmW4TDWqez1hf div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-scGJmW4TDWqez1hf :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

获取http输出流
基于1创建压缩文件写入流
遍历文件
将文件写入压缩文件流
写入完毕

这种的方法是直接遍历文件写入http的响应流,不用在服务器上再创建一个压缩文件,节省了删除文件的操作。而且,没有再出现压缩文件损坏的情况。具体实现代码如下:

 @GetMapping("/downloadByPath")public void fileDownByPath(@RequestParam String sourceFilePath,HttpServletResponse response) {try {String fileName = "result.zip";response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "utf-8"));response.setHeader("Access-Control-Expose-Headers", "content-disposition");File sourceFile = new File(sourceFilePath);if (!sourceFile.exists()) {System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");} else {ZipOutputStream zos = null;BufferedInputStream bis = null;try{File[] files = sourceFile.listFiles();zos = new ZipOutputStream(response.getOutputStream());byte[] buf = new byte[8192];int len;for (int i = 0; i < files.length; i++) {File file = files[i];if (!file.isFile()) continue;ZipEntry ze = new ZipEntry(file.getName());zos.putNextEntry(ze);bis = new BufferedInputStream(new FileInputStream(file));while ((len = bis.read(buf)) > 0) {zos.write(buf, 0, len);}zos.closeEntry();}zos.closeEntry();}catch(Exception ex){ex.printStackTrace();}finally {if(bis != null){try{bis.close();}catch(Exception e){e.printStackTrace();}}if(zos != null){try{zos.close();}catch(Exception e){e.printStackTrace();}}}}} catch (Exception e) {e.printStackTrace();System.out.println("下载文件失败");response.setStatus(500);}}

Springboot 实现将文件压缩,然后下载相关推荐

  1. javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)

      程序员在做web等项目的时候,往往都需要添加文件上传.下载.删除的功能,有时是单文件,有时多文件批量 操作,而这些功能的代码程序员可以自己收藏起来当成工具使用,这样,程序员在进行程序设计的时候就会 ...

  2. Java将一个文件夹下多个文件压缩并下载(工作案例)

    Java将一个文件下多个文件压缩并下载,文件夹目录如下: 每个文件下都有文件,要求实现将文件夹"A2023001_检查"压缩成"A2023001.zip",如下 ...

  3. springboot单文件下载和多文件压缩zip下载

    微信搜索:"二十同学" 公众号,欢迎关注一条不一样的成长之路 单文件下载 //下载单个文件 public void downloadFile(HttpServletResponse ...

  4. springboot:实现文件上传下载实时进度条功能【附带源码】

    0. 引言 记得刚入行的时候,做了一个文件上传的功能,因为上传时间较久,为了用户友好性,想要添加一个实时进度条,显示进度.奈何当时技术有限,查了许久也没用找到解决方案,最后不了了之. 近来偶然想到这个 ...

  5. java文件下载接口_javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)...

    @RequestMapping("/uploads")public void recursion(String root, Vector vecFile,HttpServletRe ...

  6. 实现web多层级文件压缩并下载

    话不多说直接上代码 文件拷贝,压缩多个文件到指定位置,压缩文件夹及以下子集,web段文件下载的工具类 public class DownFileUtil {/*** @description: 压缩文 ...

  7. Java把文件压缩然后下载

    一边压缩一边下载,前端写法可以参考Java通过流下载文件以及相关优化 response.setContentType("application/octet-stream");res ...

  8. SpringBoot实现多文件压缩下载.zip

    前言:服务器生成多个文本文件.需要打包一次返回下载.例如:xml,word,excel try {response.setHeader("Content-Disposition", ...

  9. 继上一篇博客--javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)

    通过动态分配地址来提升javaweb文件下载接口的其兼容性和可扩展性: (上篇博文地址:https://blog.csdn.net/weixin_37766296/article/details/80 ...

最新文章

  1. golang 操作 ini配置文件
  2. Jacobi并行拆解【补充】
  3. python 获取帮助页_Python-爬取页面内容(涉及urllib、requests、UserAgent、Json等)
  4. 数学建模1---基本方法和步骤
  5. arcmap新手教程_ArcGIS入门教程(1)——ArcMap应用基础
  6. 程序员必备75道逻辑思维题(附答案)之二
  7. VPS、云服务器(云主机)和虚拟主机有何异同?
  8. 一天一个西红柿,健康多多
  9. 关于offer、三方协议、福利等就业常识
  10. JavaScript事件与JQuery事件
  11. python中交集并集用什么符号表示_Python实现两个list求交集,并集,差集的方法示例...
  12. git hub寻找资源
  13. 别人的面经(算法方向)
  14. Python下数值型与字符型类别变量独热编码(One-hot Encoding)实现
  15. BTree和B+Tree详解
  16. php框架静音箱,静音测试箱
  17. Vue.js:使用v-bind为多个元素绑定style样式案例
  18. java model类作用_SPRING框架中ModelAndView、Model、ModelMap区别及详细分析
  19. 6.又双叒叕背包模拟
  20. matlab计算空间卷积,如何在MATLAB和WolframAlpha中计算函数与自身的卷积?

热门文章

  1. 随机变量的期望和方差
  2. dropna()函数如何使用
  3. Oracle 10g 32位 下载地址
  4. BlueHost主机从零开始使用笔记,我踩过的坑你就不要踩了.
  5. 网站优化推广怎么做?
  6. php silk文件转换pcm,微信语音silk格式文件转换处理记录
  7. PyEcharts数据可视化(1)——配置项
  8. Unity下实现弹簧骨骼(Spring Bone)
  9. Matlab中 的@(x)——处理匿名函数
  10. const* 和 *const