多文件下载并打成zip文件传输到浏览器响应流

2023-03-14,,

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; @Slf4j
public class FileZipUtil { private static final String utf8 = "UTF-8"; /**
* 下载zip文件
* @param srcfile
* @param zipFileName
* @param response
*/
public static void createZipByBytes(List<Map<String, Object>> srcfile, String zipFileName, HttpServletResponse response) {
byte[] buf = new byte[1024];
BufferedOutputStream bos;
InputStream in = null;
ZipOutputStream out = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
response.reset();
response.setCharacterEncoding("utf-8");
response.addHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Expose-Headers", "*");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, utf8));
response.setContentType("application/octet-stream;charset=UTF-8");
out = new ZipOutputStream(bos);
for (int i = 0; i < srcfile.size(); i++) {
byte[] bytes = (byte[]) srcfile.get(i).get("content");
in = new ByteArrayInputStream(bytes);
out.putNextEntry(new ZipEntry(srcfile.get(i).get("fileName").toString()));
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
out.close();
bos.close();
log.info("压缩完成");
} catch (Exception e) {
log.error("zip文件打包异常{}",e.getMessage());
throw new BusinessException("zip文件打包异常");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 下载zip文件
* @param srcfile
* @param zipFileName
* @param response
*/
public static void createZipByFiles(List<File> srcfile,String zipFileName,HttpServletResponse response) {
byte[] buf = new byte[1024];
// 获取输出流
BufferedOutputStream bos;
FileInputStream in = null;
ZipOutputStream out = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
response.reset();
response.setCharacterEncoding("utf-8");
response.addHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Expose-Headers", "*");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, utf8));
response.setContentType("application/octet-stream;charset=UTF-8");
out = new ZipOutputStream(bos);
for (int i = 0; i < srcfile.size(); i++) {
in = new FileInputStream(srcfile.get(i));
out.putNextEntry(new ZipEntry(srcfile.get(i).getName()));
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
out.close();
bos.close();
log.info("压缩完成");
} catch (Exception e) {
log.error("zip文件打包异常{}",e.getMessage());
throw new BusinessException("zip文件打包异常");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

多文件下载并打成zip文件传输到浏览器响应流的相关教程结束。

《多文件下载并打成zip文件传输到浏览器响应流.doc》

下载本文的Word格式文档,以方便收藏与打印。