List<Map<String, Object>> zipEntry = new LinkedList<>();
for (File file : fileList) {
zipEntry.add(getDocMap(file.getName(), file));
}
String pathname = "批量导出建议详情__" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
response.setContentType("application/zip");
try {
response.addHeader("Content-Disposition", "attachment;filename=" + new String(pathname.getBytes("utf-8"), "ISO8859-1"));
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Location", pathname);
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
for (int i = 0; i < zipEntry.size(); i++) {
ZipEntry zipEntryDetail = (ZipEntry) zipEntry.get(i).get("zip");
InputStream in = (InputStream) zipEntry.get(i).get("stream");
out.putNextEntry(zipEntryDetail);
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
log.error("", e);
} finally {
try {
response.flushBuffer();
} catch (IOException e) {
log.error("", e);
}
FileUtil.deleteFile(fileList.get(0).getParentFile());
}
public Map<String, Object> getDocMap(String fileName, File file) {
Map<String, Object> map = new HashMap<>(3);
InputStream is = null;
try {
is = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
map.put("stream", is);
map.put("zip", zipEntry);
} catch (FileNotFoundException e) {
log.error("", e);
}
return map;
}