struts實現下載篇

xuehongliang發表於2007-06-29
java 程式碼 public class DownLoadAction extends Action { // --------------------------------------------------------- Instance // Variables BufferedInputStream bis = null; BufferedOutputStream bos = null; // --------------------------------------------------------- Methods /** * Method execute * * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String downloadPath = servlet.getInitParameter("file-upload"); String fileName = new String(request.getParameter("filename").getBytes( "ISO-8859-1"), "GB2312"); response.reset(); response.setContentType("application/x-msdownload"); response.addHeader("Content-Disposition", "attachment; filename="" + new String(fileName.getBytes("GB2312"), "ISO-8859-1") + """); try { bis = new BufferedInputStream(new FileInputStream(downloadPath + "/" + fileName)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[20480]; while ((bis.read(buff, 0, buff.length)) != -1) { bos.write(buff, 0, buff.length); } bos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { bos.close(); bis.close(); } return mapping.findForward("downloadfile"); } } web.xml xml 程式碼 action org.apache.struts.action.ActionServlet config /WEB-INF/conf/struts-config.xml config/login /WEB-INF/conf/login/struts-config-login.xml debug 3 detail 3 file-upload E:/upload 2 Set Character Encoding com.capinfo.filter.SetCharacterEncodingFilter encoding GB2312 Set Character Encoding /* action *.do /page/welcome.jsp /WEB-INF/struts-bean.tld /WEB-INF/struts-bean.tld /WEB-INF/struts-html.tld /WEB-INF/struts-html.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-template.tld /WEB-INF/struts-template.tld /WEB-INF/struts-tiles.tld /WEB-INF/struts-tiles.tld /WEB-INF/struts-nested.tld /WEB-INF/struts-nested.tld struts-config xml 程式碼
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/800861/viewspace-922102/,如需轉載,請註明出處,否則將追究法律責任。

相關文章