下载:
java 代码
- public class DownLoadAction extends Action {
-
-
-
- BufferedInputStream bis = null;
-
- BufferedOutputStream bos = null;
-
-
-
-
-
-
-
-
-
-
-
-
- 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 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app>
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/conf/struts-config.xml</param-value>
- </init-param>
- <init-param>
- <param-name>config/login</param-name>
- <param-value>/WEB-INF/conf/login/struts-config-login.xml</param-value>
- </init-param>
- <init-param>
- <param-name>debug</param-name>
- <param-value>3</param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value>3</param-value>
- </init-param>
- <init-param>
- <param-name>file-upload</param-name>
- <param-value>E:/upload</param-value>
- </init-param>
- <load-on-startup>2</load-on-startup>
- </servlet>
-
- <filter>
- <filter-name>Set Character Encoding</filter-name>
- <filter-class>com.capinfo.filter.SetCharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>GB2312</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>Set Character Encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
-
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
-
-
-
- <welcome-file-list>
- <welcome-file>/page/welcome.jsp</welcome-file>
- </welcome-file-list>
-
- <taglib>
- <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
- </taglib>
-
- </web-app>
-
struts-config
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
-
- <struts-config>
-
- <form-beans>
- <form-bean name="uploadForm" type="com.capinfo.form.upload.UploadForm" />
- </form-beans>
-
- <global-forwards>
- <forward name="welcome" path="/Welcome.do" />
- <forward name="login" contextRelative="true" path="/login/Login.do" redirect="true" />
- <forward name="upload" path="/page/upload.jsp" redirect="true"/>
- </global-forwards>
-
- <action-mappings>
-
- <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/page/Welcome.jsp"></action>
-
- <action path="/upload" type="com.capinfo.action.upload.UploadAction" name="uploadForm" input="/page/upload.jsp" scope="request">
- <forward name="success" path="/page/upload.jsp" />
- </action>
-
- <action path="/downLoad"
- type="com.yourcompany.struts.action.DownLoadAction"
- input="/page/upload.jsp"
- scope="request"
- >
- <forward name="downloadfile" path="/page/upload.jsp" />
- </action>
-
- </action-mappings>
-
- <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
-
- </struts-config>
-
|
http://xuehongliang.javaeye.com/blog/53901
http://community.csdn.net/Expert/topic/4964/4964004.xml?temp=.5579492