盒子
盒子

JAVA流实现文件批量打包下载

话不多说,直接撸代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@ResponseBody
public void downloadFiles(HttpServletRequest request, HttpServletResponse response, String[] filePaths) {
if (filePaths == null || filePaths.length <= 0) {
return ;
}

// 设置响应头
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");

// 设置压缩包名称及不同浏览器中文乱码处理
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime localDateTime = LocalDateTime.now();
String filename = "电子合同" + formatter.format(localDateTime) + ".zip";

String agent = request.getHeader("AGENT");
try {
if (agent.contains("MSIE") || agent.contains("Trident")) {
filename = URLEncoder.encode(filename, "UTF-8");
} else {
filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\""); // key不区分大小写

try {
// 设置压缩流,直接写入response,实现边压缩边下载
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipOutputStream.setMethod(ZipOutputStream.DEFLATED);

DataOutputStream dataOutputStream = null;
for (String filePath : filePaths) {
String subFilename = formatter.format(LocalDateTime.now()) + filePath.substring(filePath.lastIndexOf("/") + 1);
zipOutputStream.putNextEntry(new ZipEntry(subFilename));

dataOutputStream = new DataOutputStream(zipOutputStream);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
byte[] buf = new byte[8192];
int length = 0;

while ((length = bufferedInputStream.read(buf)) != -1) {
dataOutputStream.write(buf, 0 , length);
}
dataOutputStream.flush();
// dataOutputStream.close(); 若在此关闭,对应资源zipOutputStream也将关闭,则压缩包内仅有一个文件
bufferedInputStream.close();
zipOutputStream.closeEntry();
}

dataOutputStream.flush();
dataOutputStream.close();
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
支持一下
扫一扫,支持沈健
  • 微信扫一扫
  • 支付宝扫一扫