Struts中的文件上传

还是代码说话吧。。

首先是web.xml文件

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 解决中文乱码问题 --> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.jadyer.util.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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/struts-config.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> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>upload.jsp</welcome-file> </welcome-file-list> </web-app>

然后是上传文件的upload.jsp页面

<%@ page language="java" pageEncoding="UTF-8"%> <form action="upload.do" method="POST" enctype="multipart/form-data"> 标题:<input type="text" name="title" /><br/> 文件:<input type="file" name="myfile" /><br/> <input type="submit" value="上传文件" /> </form>

用于显示文件上传结果的result.jsp页面

<%@ page language="java" pageEncoding="UTF-8"%> <h2>请到D盘根目录下查看所上传的文件</h2> <br/> 名称:${uploadForm.title}<br> 文件:${uploadForm.myfile.fileName}

然后是Struts1.x的核心配置文件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.jadyer.struts.form.UploadForm" /> </form-beans> <action-mappings> <action path="/upload" type="com.jadyer.struts.action.UploadAction" name="uploadForm" scope="request"> <forward name="success" path="/result.jsp" /> </action> </action-mappings> <!-- 配置上传参数:设定所上传的文件的最大容量 --> <controller maxFileSize="10M" /> </struts-config>

用到的Struts1.x中的ActionForm类

package com.jadyer.struts.form; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; /** * @see FormFile是由Struts封装的一个接口,它是Struts中专门用来处理文件上传的 * @see 故Struts上传文件时ActionForm中必须使用FormFile类型来接收上传的文件 * @see 然后在Action中调用FormFile取得上传文件数据,采用流输出,即完成上传 */ @SuppressWarnings("serial") public class UploadForm extends ActionForm { private String title; private FormFile myfile; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public FormFile getMyfile() { return myfile; } public void setMyfile(FormFile myfile) { this.myfile = myfile; } }

处理上传操作的Action类

package com.jadyer.struts.action; import java.io.FileOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; import com.jadyer.struts.form.UploadForm; public class UploadAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadForm uf = (UploadForm)form; FormFile myFile = uf.getMyfile(); if (myFile != null) { System.out.println(" Title=" + uf.getTitle()); System.out.println("FileName=" + myFile.getFileName()); FileOutputStream fos = new FileOutputStream("D://" + myFile.getFileName()); //获取文件内容 //这种写法并不好。因为这是把上传的内容直接读取出来,然后都存到字节数组中 //而这部分内容是驻足在内存里的,如果文件过大的,后果可想而知 //这里只是为了演示Struts的上传功能,更专业的方法是使用其它的上传的插件 byte[] buf=myFile.getFileData(); fos.write(buf); //所谓flush的含义是把内存中的东西真正的放到物理上,写到磁盘上 fos.flush(); fos.close(); } return mapping.findForward("success"); } }

最后是用于解决中文乱码问题的Servlet过滤器

package com.jadyer.util; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 过滤器:设定传输参数编码 */ public class CharacterEncodingFilter implements Filter { private FilterConfig config; private String encoding = "ISO8859_1"; /** * 当过滤器对象被销毁的时候,由Web容器调用该方法 * @see 该方法主要用来释放过滤器对象所占用的资源 */ public void destroy() { config = null; } /** * 由Web容器在刚生成过滤器对象的时候进行调用,主要用来完成对过滤器的初始化工作 * @see 该方法接收一个FilterConfig类型的参数,通过该参数以获得有关当前过滤器的信息 * @see 例如:调用FilterConfig的getFilterName()方法可以获得过滤器的名字 * @see 例如:调用getServletContext()方法可以获得当前的ServletContext对象 * @see 这里主要是用来读取web.xml中设定的<param-name>encoding</param-name>的值 */ public void init(FilterConfig config) throws ServletException { this.config = config; String s = config.getInitParameter("encoding"); if (null != s){ encoding = s; } } /** * doFilter()方法是Filter接口中最核心的方法,完成过滤器的过滤功能 */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(encoding); //设定表单参数编码方式 chain.doFilter(request, response); } }

你可能感兴趣的:(Struts中的文件上传)