Java将多个文件打包成ZIP并下载
Java将多个文件打包成ZIP并下载
需求是多个文件需要同时打包成zip压缩文件并下载到本地,首先我需要的是知道下载文件的路径。我有一个专门的sys_file_info表,表中有对应的文件路径。业务表中的文件地址放的就是文件表的id值。下面是代码:
1.Mapper层
<select id="selectScriptPathList" parameterType="String" resultType="map">SELECT i.file_path filePath, i.file_name fileName FROM ves_data_poc pLEFT JOIN sys_file_info i ON p.script_url = i.file_idWHERE i.file_path IS NOT NULLAND p.id IN<foreach item="id" collection="array" open="(" separator="," close=")">#{id}</foreach>
</select>
List<Map<String, String>> selectScriptPathList(String[] ids);
2.Service层
idStr 是前端传来的多个id值用逗号连接的字符串
/*** 下载ZIP包* @param idStr* @param response*/
void download(String idStr, HttpServletResponse response);
@Override
public void download(String idStr, HttpServletResponse response) {if (StringUtils.isNotEmpty(idStr)) {String[] ids = idStr.split(",");List<Map<String, String>> filePaths = vesDataPocMapper.selectScriptPathList(ids);String zipName = "POC_" + ((int) (Math.random() * 10000)) + ".zip";String zipPath = Global.getProfile() + "/" + zipName;CompressUtil.compress(filePaths, zipPath, false);File pocZipFile = new File(zipPath);CompressUtil.downloadZip(response, zipName, pocZipFile);}
}
3.Controller层
/*** 下载poc文件 支持多个下载* 格式 zip包** @param ids*/
@Log(title = "数据管理-POC", businessType = BusinessType.EXPORT)
@GetMapping("/download")
public void download(String ids, HttpServletResponse response) {vesDataPocService.download(ids, response);
}
4.工具类
import org.apache.commons.lang3.StringUtils;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class CompressUtil {/*** 生成zip压缩文件* @param filePaths* @param zipFilePath* @param keepDirStructure*/public static void compress(List<Map<String, String>> filePaths, String zipFilePath, Boolean keepDirStructure) {byte[] buf = new byte[1024];File zipFile = new File(zipFilePath);try {ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));for (int i = 0; i < filePaths.size(); i++) {String relativePath = filePaths.get(i).get("filePath");String relativeName = filePaths.get(i).get("fileName");if (StringUtils.isEmpty(relativePath)) {continue;}File sourceFile = new File(relativePath);if (sourceFile == null || !sourceFile.exists()) {continue;}FileInputStream fis = new FileInputStream(sourceFile);if (keepDirStructure != null && keepDirStructure) {zos.putNextEntry(new ZipEntry(relativePath));} else {zos.putNextEntry(new ZipEntry(i + "_" + relativeName));}int len;while ((len = fis.read(buf)) > 0) {zos.write(buf, 0, len);}zos.closeEntry();// zos.close();}zos.close();if (!zipFile.exists()) {zipFile.createNewFile();}} catch (Exception e) {e.printStackTrace();}}/*** 下载zip** @param response* @param zipName 浏览器header中zip名称* @param zipFile zipFile文件*/public static void downloadZip(HttpServletResponse response, String zipName, File zipFile) {//下载文件try {response.setCharacterEncoding("utf-8");response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment;FileName=" + zipName);ServletOutputStream out = response.getOutputStream();int len = 0;byte[] buffer = new byte[1024];FileInputStream fis = new FileInputStream(zipFile);while ((len = fis.read(buffer)) > 0) {out.write(buffer, 0, len);out.flush();}out.close();fis.close();response.flushBuffer();} catch (IOException e) {e.printStackTrace();}}
}
Java将多个文件打包成ZIP并下载相关推荐
- 关于springmvc下服务器文件打包成zip格式下载功能
关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储 版权声明:本文为博主原创 ...
- spring mvc java 把多文件打包成zip,并下载
再来一篇 /*** 压缩并导出文件* @param zipPath 压缩文件临时路径 路径最后不要有 /* @param zipName 压缩为文件名 **.zip* @param createFil ...
- php 复制文件夹并压缩到最小_php压缩多个文件打包成zip并下载到本地
完成时间:2018-01-03 展现方式:整个相册图片的导出(下载到本地)直接在页面上生成下载没有什么另存为什么默认路径 基本步骤: 1.用户点击按钮跳转到对应相册导出的方法 ---->2.在方 ...
- SpringBoot将文件打包成zip存放或导出
目录 前言 环境准备 将文件打包成Zip存放 代码 测试 将文件打包成zip并导出 代码 测试 结尾 前言 相信各位看官在工作中都会遇到过要把多个文件打包成一个压缩文件然后导出,或者将文件打包成Zip ...
- java 文件流的处理 文件打包成zip
1.下载文件到本地 public void download(HttpServletResponse response){String filePath ="";//文件路径Str ...
- java 把文件打包成zip_java 文件流的处理 文件打包成zip
1.下载文件到本地 public void download(HttpServletResponse response){ String filePath ="";//文件路径 S ...
- 文件打包成zip类型文件
研究了一下,如何把文件打包成Zip文件. 下面是我经历的进程: 1. 首先看到了微软本身的打包代码,是通过System.IO.Packaging 命令空间来进行打包,你可以点击以上的链接来查看其如何打 ...
- python打包zip文件_python 解压文件,合并文件 打包成zip格式文件 生成MD5值
#!/usr/bin/env python #_*_encoding:utf-8 # 2018/05/29 #augustyang #2.0 ''' 解压文件,合并文件 打包成zip格式文件 生成MD ...
- think.class.php下载,PHP_ThinkPHP实现将本地文件打包成zip下载,首先,将FileToZip.class文件放到T - phpStudy...
ThinkPHP实现将本地文件打包成zip下载 首先,将FileToZip.class文件放到ThinkPHP/Extend/Library/ORG/Util/文件夹中,FileToZip.class ...
最新文章
- 机器学习的两种计算范式
- JavaScript 开发者数量暴涨、C# 超越 PHP,揭晓全球开发最新趋势!
- 平均数、中位数和众数及它们之间的关系
- 想要提升网站曝光率应避免哪些错误设置的出现?
- audio标签下方有空隙的原因与解决方法
- PHP中生成UUID
- 面试官系统精讲Java源码及大厂真题 - 18 场景集合:并发 List、Map的应用场景
- ------webkitformboundary
- 如果现在单位让你内退,拿基本工资,不用上班,你是否愿意?
- 区别德语的公母阴阳性别的秘诀
- 原来闭包也可以这么简单
- VRay4.2 for 3dsMax2013-2020
- 简单的js在html页面打印机,在javaScript中如何连接打印机
- 带通滤波器幅频特性曲线图_滤波器知识,你所要的,都在这里
- ret-sync插件:windbg/ollydbg+ida逆向调试神器
- 医院管理系统明细 HIS源码
- potplayer默认专辑加载同一文件夹下所有视频,只读取一部分视频,加载视频目录不完整
- 读论文:Charting the Right Manifold:Manifold Mixup for Few-shot Learning
- [渗透测试学习靶机02] vulnhub靶场 Empire: Breakout
- winXP自动关机命令