存一波打包代码
参考博文:https://blog.csdn.net/ajax_mck/article/details/111320900
/**
* @param title 压缩包 title
* @param response
* @param fileList 文件集合
* @return
* @Title downFileZip
* @Description: 浏览器批量下载文件打包
* @throws:
* 思路:先生成多个文件存至虚拟路径,然后调用此方法,最后再删除生成的多个文件
*/
public boolean downFileZip(String title, HttpServletResponse response, List<File> fileList) throws UnsupportedEncodingException {
boolean flag = true;
response.reset();
response.setContentType("multipart/form-data");
//设置压缩包名
String strZipPath = "压缩包";
strZipPath = URLEncoder.encode(strZipPath + ".zip", "UTF-8");
//处理文件名空格转加号
strZipPath = strZipPath.replace("+", "%20");
// 文件头,不同浏览器需要处理乱码
response.setHeader("Content-disposition", "attachment;filename="
+ strZipPath);
try (
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
DataOutputStream os = new DataOutputStream(zipOutputStream)
) {
zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
//设置conkie关闭进度条显示
//CommonUtil.setResponseCookie(response);
if (!CollectionUtils.isEmpty(fileList)) {
for (File file : fileList) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
int len;
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
os.write(b, 0, len);
}
fis.close();
}
}
} catch (Exception e) {
logger.error("压缩失败", e);
flag = false;
}
return flag;
}