Struts1中文件下载
2011年09月01日
网上复制一份代保存
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { FileDownLoadForm fdform =(FileDownLoadForm)form; String path = fdform.getFilePath(); response.setContentType("application/x-download"); String filenamedownload = path; String filenamedisplay = path.substring(path.lastIndexOf("/")+1); try { filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8"); response.addHeader("Content-Disposition","attachme nt;filename=" + filenamedisplay); RequestDispatcher dispatcher = request.getRequestDispatcher(filenamedownload); if(dispatcher != null) { dispatcher.forward(request,response); } response.flushBuffer(); return null ; // return mapping.findForward("success"); }catch(FileNotFoundException e){ e.printStackTrace(); return mapping.findForward("error"); }catch(Exception e){ e.printStackTrace(); return null; } }
2011年09月01日
网上复制一份代保存
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { FileDownLoadForm fdform =(FileDownLoadForm)form; String path = fdform.getFilePath(); response.setContentType("application/x-download"); String filenamedownload = path; String filenamedisplay = path.substring(path.lastIndexOf("/")+1); try { filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8"); response.addHeader("Content-Disposition","attachme nt;filename=" + filenamedisplay); RequestDispatcher dispatcher = request.getRequestDispatcher(filenamedownload); if(dispatcher != null) { dispatcher.forward(request,response); } response.flushBuffer(); return null ; // return mapping.findForward("success"); }catch(FileNotFoundException e){ e.printStackTrace(); return mapping.findForward("error"); }catch(Exception e){ e.printStackTrace(); return null; } }