实现批量压缩图片并下载
String zipFileName = “D:/developPJ/ams/workspace/ArchivesPJ/archives/src/main/webapptemp_download/test.zip”;
File zipFile = new File(zipFileName);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
// zip的名称为
zipOut.setComment(“test.zip”);
List<File> fileList = new ArrayList<File>();
List<AmsImagesCw> imgList = financeService.selectAmsImagesCw(archiveId);
for (AmsImagesCw ImagesCw : imgList) {
fileList.add(new File(ImagesCw.getImagePath()));
for (int i = 0; i < fileList.size(); ++i) {
File file1 = fileList.get(i);
zipOut.putNextEntry(new ZipEntry(file1.getName()));
zipOut.write(FileUtils.readFileToByteArray(file1));
}
String filePath = zipFileName;
File file = new File(filePath);
String filename = “test.zip”;
FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream();
response.setHeader(“Content-Disposition”, “attachment;filename=”
+ new String(filename.getBytes(“gb2312”), “iso8859-1”));
response.setCharacterEncoding(“utf-8”);
os.write(FileUtils.readFileToByteArray(zipFile));
os.flush();
fis.close();
zipOut.close();
// 输出客户端结束后,删除压缩包
if (zipFile.exists()) {
zipFile.delete();
}