ITPub博客

首页 > 应用开发 > IT综合 > struts实现下载篇

struts实现下载篇

原创 IT综合 作者:xuehongliang 时间:2007-06-29 08:39:33 0 删除 编辑
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博客 ” ,链接:https://blog.itpub.net/800861/viewspace-922102/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: struts实现上传篇
请登录后发表评论 登录
全部评论
  • 博文量
    40
  • 访问量
    391221