java实现下载全部附件功能

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<attachList.size(); 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值