java实现下载全部附件功能

2019-04-15 13:45发布

gmail里有下载全部附件功能:

[img]../../upload/picture/pic/8758/3be2c4a2-7cb4-39b8-b0bc-c55d4c57adc6.jpg" border="0" alt="[/img]

[img]../../upload/picture/pic/8756/be8bdb27-f0c0-37f9-8c84-0e6f13c7c05f.jpg" border="0" alt="[/img]

粗略的实现了下,主要代码如下:


public ActionForward processActionDownloadAllAttach(
ActionMapping mapping,
ActionForm formIn,
HttpServletRequest request,
HttpServletResponse response,
ActionErrors errors) {
HttpSession session = request.getSession(false);
AttachForm form = (AttachForm) formIn;
ActionForward actionForward = new ActionForward(mapping.getInput());
try {
ArrayList attachList = form.getAttachList();
OutputStream out = response.getOutputStream();
ZipOutputStream zout = new ZipOutputStream(out);
response.setContentType("application/x-zip-compressed");
response.setStatus(response.SC_OK);
response.setHeader("Content-Disposition", "attachment;filename="ok.zip"");
for(int i=0; attachList!=null && i AttachRow row = (AttachRow) attachList.get(i);
String filePath = row.getAttachFilePath();
String fileName = filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
File file = new File(row.getAttachFilePath());
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zout.putNextEntry(zipEntry);
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
zout.write(buffer, 0, bytesRead);
}
zout.closeEntry();
}
zout.close();
out.close();
actionForward = null;
} catch (Exception e) {
Logger.logExceptions(this, e);
}
return actionForward;
}