目录
一、 概述
开发过程中碰到一个需求,需要将服务器上的多个文件打包为zip,并进行下载响应到客户端,写了一个Demo总结分享一下,如果有错误希望大家指正!
二、代码功能实现
这里实现只是模式本地文件下载Zip,响应的客户端下载
实现思路
- 创建一个临时文件zip
- 构建一个Zip文件输出流
- 从服务读取文件流放入Zip文件输出流
- 把临时文件Zip写入OutputStream
- 关闭资源
1. controller
/**
* 下载数据包
*
* @param response
* @date: 2021/3/25 15:36
* @return: void
*/
@GetMapping("downloadDataZip")
public void downloadDataZip(HttpServletResponse response) {
String title = "报告源数据包.zip";
File filePath = new File(FileUtil.getTemplatePath() + File.separator + title);
List<String> srcFiles = Arrays.asList("20210113001.jpg", "20210113003.jpg", "20210113004.jpg", "瞳孔.txt");
FileUtil.zipFiles(srcFiles, filePath);
String filename = System.currentTimeMillis()+"_"+title;
//设置文件路径
if (filePath.exists()) {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
byte[] buffer = new byte[4096];
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
// 删除临时文件
filePath.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2. FileUtil
public class FileUtil {
/**
* 多图片压缩zip
*
* @param srcFiles 图片名称
* @param zipFile 文件路径
*/
public static void zipFiles(List<String> srcFiles, File zipFile) {
// 判断压缩后的文件存在不,不存在则创建
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建 FileOutputStream 对象
FileOutputStream fileOutputStream ;
// 创建 ZipOutputStream
ZipOutputStream zipOutputStream ;
// 创建 FileInputStream 对象
BufferedInputStream bis = null;
FileInputStream inputStream = null;
try {
// 实例化 FileOutputStream 对象
fileOutputStream = new FileOutputStream(zipFile);
// 实例化 ZipOutputStream 对象
zipOutputStream = new ZipOutputStream(fileOutputStream);
// 创建 ZipEntry 对象
ZipEntry zipEntry ;
// 遍历源文件数组
for (String file : srcFiles) {
// 将源文件数组中的当前文件读入 FileInputStream 流中
String fileName = file;
String localFileDir = "D:\\file\\video";
fileName = URLDecoder.decode(fileName, "UTF-8");
//获取文件输入流 localFileDir是服务端存储文件的路径
File files = new File(localFileDir + File.separator + fileName);
inputStream = new FileInputStream(files);
// 文件后缀名称
// 实例化 ZipEntry 对象,源文件数组中的当前文件
zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
// 该变量记录每次真正读的字节个数
int len;
// 定义每次读取的字节数组
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取类路径
* return 绝对路径地址
*/
public static String getTemplatePath() {
String realPath = FileUtil.class.getClassLoader().getResource("").getFile();
File file = new File(realPath);
realPath = file.getAbsolutePath();
try {
realPath = java.net.URLDecoder.decode(realPath, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return realPath;
}
}
3. 前端页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>下载功能</title>
<!-- <script th:src="@{/js/jquery-3.3.1.js}"></script>-->
<script src="../static.js/jquery-3.3.1.js"></script>
</head>
<body>
<div>
<h3>下载文件测试</h3>
<!-- <button type="submit" id="submit">上传</button>-->
<a href="/order/sendUploadVoice" class="tab-down-button">下载1</a>
<a href="/order/downloadUser" class="tab-down-button">下载2</a>
<a href="/order/downloadDataZip" class="tab-down-button">报告源数据包</a>
<button type="button" onclick="downloadzip()">按钮下载</button>
<!--预览图片-->
<div class="preview_box"></div>
</div>
<script type="text/javascript">
function downloadzip(){
window.location.href = "/order/downloadDataZip"
};
</script>
</body>
</html>