SpringBoot入门:SpringBoot文件批量zip打包下载

平时开发中文件的上传下载经常会遇到,如excel的上传下载等,Excel的处理可以使用阿里的EasyExcel,一款快速、简单避免OOM的java处理Excel工具,github:https://github.com/alibaba/easyexcel, 有兴趣的可以自己去尝试下;

此处介绍文件的批量下载, 如果一次性下载多个文件的话, 建议生成文件后将文件打成ZIP包,再下载,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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;


public class ZipDownloadUtil {

private static final Logger logger = LoggerFactory.getLogger(ZipDownloadUtil.class);

/**
* 获取当前系统的临时目录
*/
private static final String FILE_PATH = System.getProperty("java.io.tmpdir") + File.separator;

private static final int ZIP_BUFFER_SIZE = 8192;

/**
* zip打包下载
*
* @param response
* @param zipFileName
* @param fileList
*/
public static void zipDownload(HttpServletResponse response, String zipFileName, List<File> fileList) {
// zip文件路径
String zipPath = FILE_PATH + zipFileName;
try {
//创建zip输出流
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath))) {
//声明文件集合用于存放文件
byte[] buffer = new byte[1024];
//将文件放入zip压缩包
for (int i = 0; i < fileList.size(); i++) {
File file = fileList.get(i);
try (FileInputStream fis = new FileInputStream(file)) {
out.putNextEntry(new ZipEntry(file.getName()));
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
}
}
}
//下载zip文件
downFile(response, zipFileName);
} catch (Exception e) {
logger.error("文件下载出错", e);
} finally {
// zip文件也删除
fileList.add(new File(zipPath));
deleteFile(fileList);
}
}


/**
* 文件下载
*
* @param response
* @param zipFileName
*/
private static void downFile(HttpServletResponse response, String zipFileName) {
try {
String path = FILE_PATH + zipFileName;
File file = new File(path);
if (file.exists()) {
try (InputStream ins = new FileInputStream(path);
BufferedInputStream bins = new BufferedInputStream(ins);
OutputStream outs = response.getOutputStream();
BufferedOutputStream bouts = new BufferedOutputStream(outs)) {
response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
int bytesRead = 0;
byte[] buffer = new byte[ZIP_BUFFER_SIZE];
while ((bytesRead = bins.read(buffer, 0, ZIP_BUFFER_SIZE)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();
}
}
} catch (Exception e) {
logger.error("文件下载出错", e);
}
}

/**
* 删除文件
*
* @param fileList
* @return
*/
public static void deleteFile(List<File> fileList) {
for (File file : fileList) {
if (file.exists()) {
file.delete();
}
}
}
}

SpringBoot web层代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class FileUploadController {

@RequestMapping(value = "zipDownload", method = RequestMethod.GET)
public String zipDownload(HttpServletRequest request, HttpServletResponse response) {
List<File> files = new ArrayList<>();
files.add(new File("D:\\demo1.xlsx"));
files.add(new File("D:\\demo2.xlsx"));
ZipDownloadUtil.zipDownload(response, "demo.zip", files);
return "download success";
}

}

SpringBoot入门:SpringBoot文件批量zip打包下载
https://river106.cn/posts/1144f0d3.html
作者
river106
发布于
2020年12月15日
许可协议