import org.apache.poi.xssf.usermodel.XSSFWorkbook;标签:zipOutputStream,outputStream,java,zip,excel,import,response,压缩包 From: https://www.cnblogs.com/myself1012/p/17546543.html
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void zipFile(HttpServletResponse response, List<XSSFWorkbook> workbookList, String fileName, List<String> file) throws IOException {
ServletOutputStream outputStream = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
try {
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename= " + URLEncoder.encode(fileName, "UTF-8") + ".zip");
for (int i = 0; i < workbookList.size(); i++) {
XSSFWorkbook workbook = workbookList.get(i);
String str = file.get(i);
ZipEntry zipEntry = new ZipEntry(str);
zipOutputStream.putNextEntry(zipEntry);
workbook.write(zipOutputStream);
}
zipOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipOutputStream != null) {
zipOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}