struts1.x+spring的文件上传和下载的实现
项目需要在struts1.x+spring2.5环境下实现 上传下载模块。 现总结如下:
前台页面
在jsp页面中添加
<html:form action="/upload" method="post" enctype="multipart/form-data"> <html:text property="name"></html:text> <html:file property="myFile"></html:file> <html:submit></html:submit> </html:form>
注意:数据一定要以enctype="multipart/form-data" 形式提交上去
配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 <display-name>HelloApp</display-name> 8 9 <welcome-file-list> 10 <welcome-file>upload.jsp</welcome-file> 11 </welcome-file-list> 12 <!--spring listener --> 13 <listener> 14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 15 </listener> 16 <!--spring listener end --> 17 <!-- struts start --> 18 <servlet> 19 <servlet-name>action</servlet-name> 20 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 21 <init-param> 22 <param-name>config</param-name> 23 <param-value>/WEB-INF/struts-config.xml</param-value> 24 </init-param> 25 <load-on-startup>2</load-on-startup> 26 </servlet> 27 28 <servlet-mapping> 29 <servlet-name>action</servlet-name> 30 <url-pattern>*.do</url-pattern> 31 </servlet-mapping> 32 <!-- struts end --> 33 34 <!-- 文字过滤器 start --> 35 <filter> 36 <filter-name>FilterChar</filter-name> 37 <filter-class> 38 com.fileupload.FilterChar 39 </filter-class> 40 <init-param> 41 <param-name>chars</param-name> 42 <param-value>utf-8</param-value> 43 </init-param> 44 </filter> 45 <filter-mapping> 46 <filter-name>FilterChar</filter-name> 47 <url-pattern>/*</url-pattern> 48 </filter-mapping> 49 50 <!-- 文字过滤器 end --> 51 52 </web-app>
配置Struts-Config.xml
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE struts-config PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 4 "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 5 6 <struts-config> 7 8 <form-beans> 9 <form-bean name="upLoadForm" type="com.fileupload.UpLoadActionForm"> 10 </form-bean> 11 </form-beans> 12 13 <action-mappings> 14 <action path="/upload" name="upLoadForm" 15 type="org.springframework.web.struts.DelegatingActionProxy" scope="request" 16 validate="false" parameter="action"> 17 <forward name="success" path="/success.jsp"></forward> 18 </action> 19 </action-mappings> 20 21 </struts-config>
配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-2.5.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 13 14 <bean name="/upload" class="com.fileupload.UpLoadAction"> 15 </bean> 16 </beans>
FilterChar.java 过滤乱码的类,可以通过其他方式解决乱码
1 package com.fileupload; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 /** 15 * 以 UTF-8 输入输出 16 * 17 * @author admin 18 * 19 */ 20 public class FilterChar implements Filter { 21 22 private String config; 23 24 public void destroy() { 25 this.config = null; 26 } 27 28 /** 29 * name="FilterChar" <br> 30 * init-param name="config" value="utf-8"<br> 31 * mapping url-pattern="/*"<br> 32 */ 33 public void doFilter(ServletRequest request, ServletResponse response, 34 FilterChain chain) throws IOException, ServletException { 35 HttpServletRequest req; 36 req = (HttpServletRequest) request; 37 38 HttpServletResponse res; 39 res = (HttpServletResponse) response; 40 41 req.setCharacterEncoding(config); 42 res.setCharacterEncoding(config); 43 chain.doFilter(req, res); 44 45 } 46 47 /** 48 * 获得web.xml中初始化的参数 49 */ 50 public void init(FilterConfig config) throws ServletException { 51 this.config = config.getInitParameter("chars"); 52 } 53 54 }
UploadAction.java
1 package com.fileupload; 2 3 import java.io.FileOutputStream; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.apache.struts.action.Action; 9 import org.apache.struts.action.ActionForm; 10 import org.apache.struts.action.ActionForward; 11 import org.apache.struts.action.ActionMapping; 12 import org.apache.struts.upload.FormFile; 13 14 public class UpLoadAction extends Action { 15 @Override 16 public ActionForward execute(ActionMapping mapping, ActionForm form, 17 HttpServletRequest request, HttpServletResponse response) 18 throws Exception { 19 UpLoadActionForm myForm = (UpLoadActionForm) form; 20 21 FormFile file = myForm.getMyFile(); 22 System.err.println("fileSize:" + file.getFileSize()); 23 // 待添加过滤请求 24 if (file.getFileSize() > 1024) { 25 26 } 27 28 FileOutputStream out = new FileOutputStream("e:\\" + file.getFileName()); 29 30 out.write(file.getFileData()); 31 32 out.flush(); 33 out.close(); 34 35 return mapping.findForward("success"); 36 37 } 38 }
UploadActionForm.java表单的bean对象
1 package com.fileupload; 2 3 import org.apache.struts.action.ActionForm; 4 import org.apache.struts.upload.FormFile; 5 6 public class UpLoadActionForm extends ActionForm 7 { 8 private String name; 9 private FormFile myFile; 10 11 public FormFile getMyFile() 12 { 13 return myFile; 14 } 15 16 public void setMyFile(FormFile myFile) 17 { 18 this.myFile = myFile; 19 } 20 21 public String getName() 22 { 23 return name; 24 } 25 26 public void setName(String name) 27 { 28 this.name = name; 29 } 30 31 }
任凭弱水三千,我只取一瓢饮