工具类:
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author
* @version 1.0.0
* @ClassName ZipUtil.java
* @Description 压缩文件下载工具类
* @createTime 2022年06月16日 14:59:00
*/
@Slf4j
public class ZipUtil {
private static String http ="http";
private static String https ="https";
public static class FileParam {
/**
* 文件真实路径
*/
private String realUrl;
/**
* 文件名
*/
private String fileName;
public String getFileName() {
return fileName;
}
public String getRealUrl() {
return realUrl;
}
public void setRealUrl(String realUrl) {
this.realUrl = realUrl;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
public static FileParam getFileParam(){
FileParam fileParam = new FileParam();
return fileParam;
}
/**
* 批量文件压缩下载
* @param urlList 需要批量下载文件的链接地址列表
* @param zipName 输出的压缩包名称
*/
public static void downZip(List<FileParam> urlList, String zipName,HttpServletResponse response,HttpServletRequest request){
//响应头的设置
// response.reset();
response.setContentType("application/x-msdownload");
// response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
String downloadName = zipName+".zip";
//返回客户端浏览器的版本号、类型
String agent = request.getHeader("USER-AGENT");
try {
//针对IE或者以IE为内核的浏览器:
if (agent.contains("MSIE")||agent.contains("Trident")) {
downloadName = java.net.URLEncoder.encode(downloadName, StandardCharsets.UTF_8.toString());
} else {
//非IE浏览器的处理:
downloadName = new String(downloadName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
} catch (Exception e) {
log.error("客户端浏览器版本处理异常:{}",e.getMessage());
}
response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName);
//设置压缩流:直接写入response,实现边压缩边下载
ZipOutputStream zipos = null;
try {
zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
//设置压缩方法
zipos.setMethod(ZipOutputStream.DEFLATED);
} catch (Exception e) {
log.error("设置压缩流异常:{}",e.getMessage());
}
//循环将文件写入压缩流
for (FileParam fileParam : urlList) {
try {
ZipEntry zipEntry = new ZipEntry(fileParam.getFileName());
//添加ZipEntry,并ZipEntry中写入文件流
zipos.putNextEntry(zipEntry);
InputStream inputStream =null;
if(!(fileParam.getRealUrl().startsWith(http)|| fileParam.getRealUrl().startsWith(https))){
inputStream = new FileInputStream(new File(fileParam.getFileName()));
}else {
inputStream = new URL(fileParam.getRealUrl()).openStream();
}
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
zipos.write(buffer, 0, len);
}
zipos.flush();
zipos.closeEntry();
inputStream.close();
} catch (IOException e) {
log.error("循环写入文件异常:{}",e.getMessage());
}
}
try {
zipos.close();
} catch (IOException e) {
log.error("循环写入文件异常:{}",e.getMessage());
}
}
}
调用示例:
List<ZipUtil.FileParam> fileParamList = new ArrayList<>();
//封装参数
entityList.forEach(
item ->{
ZipUtil.FileParam fileParam = ZipUtil.getFileParam();
fileParam.setFileName(item.getEquipmentName()+item.getEquipmentNum()+ StringPool.DOT+ EquipmentConstant.IMG_JPG);
fileParam.setRealUrl(item.getQrUrl());
fileParamList.add(fileParam);
}
);
ZipUtil.downZip(fileParamList, "压缩文件", response, request);