[Struts] Dowload Action
28/01/2009
/** * Download a Content. * @param mapping The action mapping * @param form The action form * @param request The requst * @param response The response * @return a valid struts mapping */ public final ActionForward download(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { byte[] content = request.getParameter("content"); String fileName = request.getParameter("fileName"); String fileContentType = request.getParameter("fileContentType"); String view = request.getParameter("view"); OutputStream out = null; try { response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "max-age=1, must-revalidate"); // This will ask the user to open or save the content (without it the browser will try to download it) if (!"true".toLowerCase().equals(view) || StringHelper.isEmpty(doc.getFileContentType())) { response.setHeader("Content-Disposition", "attachment; filename="+fileName); } response.setContentLength(content.length); if (!StringHelper.isEmpty(fileContentType)) { response.setContentType(fileContentType); } else { response.setContentType("application/octet-stream"); } out = response.getOutputStream(); out.write(content); } catch (Exception ex) { logger.error(ex.getMessage(), ex); return mapping.findForward(ERROR); } finally { if (out != null) { try { out.flush(); out.close(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } } return null; }