81,092
社区成员
发帖
与我相关
我的任务
分享
public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("entering 'AttachmentAction.download()' method...");
}
ActionMessages messages = new ActionMessages();
String id = request.getParameter("id");
String attachmentFile=request.getParameter("file");
String type = request.getParameter("type");
if (id != null||attachmentFile!=null) {
Attachment attachment =null;
if(id!=null) {
attachment= mgr.view(id);
} else if(attachmentFile!=null) {
attachment=mgr.viewByFile(attachmentFile);
}
if (attachment == null) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"object.miss"));
saveMessages(request, messages);
return mapping.findForward("failure");
}
//filename=new String(filename.getBytes("iso-8859-1"),"gb2312");
//String filepath=this.getServletContext().getRealPath("/upload");
File file = new File(mgr.getRoot()+"/"+attachment.getAttachmentFile());
String fileName = URLEncoder.encode(attachment.getAttachment(),
"UTF-8");
BufferedInputStream br = new BufferedInputStream(
new FileInputStream(file));
byte[] buf = new byte[1024 * 1024];
int len = 0;
response.reset(); //纯下载方式
//response.setContentType("application/x-msdownload");
if (type == null) {
response
.setContentType("application/octet-stream;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
} else if (type != null && type.equals("jpg")) {
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
return null;
}