struts实现下载篇

下载:

java 代码
  1. public class DownLoadAction extends Action {   
  2.   
  3.     // --------------------------------------------------------- Instance   
  4.     // Variables   
  5.     BufferedInputStream bis = null;   
  6.   
  7.     BufferedOutputStream bos = null;   
  8.   
  9.     // --------------------------------------------------------- Methods   
  10.   
  11.     /**  
  12.      * Method execute  
  13.      *   
  14.      * @param mapping  
  15.      * @param form  
  16.      * @param request  
  17.      * @param response  
  18.      * @return ActionForward  
  19.      */  
  20.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  21.             HttpServletRequest request, HttpServletResponse response)   
  22.             throws Exception {   
  23.   
  24.         String downloadPath = servlet.getInitParameter("file-upload");   
  25.         String fileName = new String(request.getParameter("filename").getBytes(   
  26.                 "ISO-8859-1"), "GB2312");   
  27.         response.reset();   
  28.         response.setContentType("application/x-msdownload");   
  29.         response.addHeader("Content-Disposition""attachment;   filename=/""  
  30.                 + new String(fileName.getBytes("GB2312"), "ISO-8859-1") + "/"");   
  31.   
  32.         try {   
  33.             bis = new BufferedInputStream(new FileInputStream(downloadPath   
  34.                     + "/" + fileName));   
  35.             bos = new BufferedOutputStream(response.getOutputStream());   
  36.   
  37.             byte[] buff = new byte[20480];   
  38.             while ((bis.read(buff, 0, buff.length)) != -1) {   
  39.                 bos.write(buff, 0, buff.length);   
  40.             }   
  41.             bos.flush();   
  42.         } catch (Exception e) {   
  43.             e.printStackTrace();   
  44.         } finally {   
  45.             bos.close();   
  46.             bis.close();   
  47.         }   
  48.   
  49.         return mapping.findForward("downloadfile");   
  50.     }   
  51.   
  52. }   

web.xml

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app>  
  3.   <servlet>  
  4.     <servlet-name>action</servlet-name>  
  5.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  6.     <init-param>    
  7.       <param-name>config</param-name>    
  8.       <param-value>/WEB-INF/conf/struts-config.xml</param-value>    
  9.     </init-param>    
  10.     <init-param>    
  11.       <param-name>config/login</param-name>    
  12.       <param-value>/WEB-INF/conf/login/struts-config-login.xml</param-value>    
  13.     </init-param>    
  14.     <init-param>  
  15.       <param-name>debug</param-name>  
  16.       <param-value>3</param-value>  
  17.     </init-param>  
  18.     <init-param>  
  19.       <param-name>detail</param-name>  
  20.       <param-value>3</param-value>  
  21.     </init-param>  
  22.     <init-param>  
  23.       <param-name>file-upload</param-name>  
  24.       <param-value>E:/upload</param-value>  
  25.     </init-param>  
  26.     <load-on-startup>2</load-on-startup>  
  27.   </servlet>  
  28.      
  29.   <filter>  
  30.     <filter-name>Set Character Encoding</filter-name>  
  31.     <filter-class>com.capinfo.filter.SetCharacterEncodingFilter</filter-class>  
  32.     <init-param>  
  33.         <param-name>encoding</param-name>  
  34.         <param-value>GB2312</param-value>  
  35.     </init-param>  
  36.   </filter>  
  37.   <filter-mapping>  
  38.     <filter-name>Set Character Encoding</filter-name>  
  39.     <url-pattern>/*</url-pattern>  
  40.   </filter-mapping>  
  41.      
  42.      
  43.   <servlet-mapping>  
  44.     <servlet-name>action</servlet-name>  
  45.     <url-pattern>*.do</url-pattern>  
  46.   </servlet-mapping>  
  47.      
  48.      
  49.      
  50.   <welcome-file-list>  
  51.     <welcome-file>/page/welcome.jsp</welcome-file>  
  52.   </welcome-file-list>  
  53.      
  54.   <taglib>  
  55.     <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>  
  56.     <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>  
  57.   </taglib>  
  58.   <taglib>  
  59.     <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>  
  60.     <taglib-location>/WEB-INF/struts-html.tld</taglib-location>  
  61.   </taglib>  
  62.   <taglib>  
  63.     <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>  
  64.     <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>  
  65.   </taglib>  
  66.   <taglib>  
  67.     <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>  
  68.     <taglib-location>/WEB-INF/struts-template.tld</taglib-location>  
  69.   </taglib>  
  70.   <taglib>  
  71.     <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>  
  72.     <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>  
  73.   </taglib>  
  74.   <taglib>  
  75.     <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>  
  76.     <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>  
  77.   </taglib>  
  78.      
  79. </web-app>  
  80.   

struts-config

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
  3.   
  4. <struts-config>  
  5.   
  6.     <form-beans>  
  7.         <form-bean name="uploadForm" type="com.capinfo.form.upload.UploadForm" />  
  8.     </form-beans>  
  9.   
  10.     <global-forwards>  
  11.         <forward name="welcome" path="/Welcome.do" />  
  12.         <forward name="login" contextRelative="true" path="/login/Login.do" redirect="true" />  
  13.         <forward name="upload" path="/page/upload.jsp" redirect="true"/>  
  14.     </global-forwards>  
  15.   
  16.     <action-mappings>  
  17.   
  18.         <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/page/Welcome.jsp"></action>  
  19.   
  20.         <action path="/upload" type="com.capinfo.action.upload.UploadAction" name="uploadForm" input="/page/upload.jsp" scope="request">  
  21.             <forward name="success" path="/page/upload.jsp" />  
  22.         </action>  
  23.            
  24.     <action path="/downLoad"    
  25.             type="com.yourcompany.struts.action.DownLoadAction"    
  26.             input="/page/upload.jsp"  
  27.             scope="request"  
  28.             >  
  29.         <forward name="downloadfile" path="/page/upload.jsp" />  
  30.     </action>  
  31.        
  32.     </action-mappings>  
  33.   
  34.     <message-resources parameter="com.yourcompany.struts.ApplicationResources" />  
  35.   
  36. </struts-config>  
  37.   
最后更新:2007-02-12 10:15

 

http://xuehongliang.javaeye.com/blog/53901

http://community.csdn.net/Expert/topic/4964/4964004.xml?temp=.5579492

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值