Java Struts2文件上传实例
项目结构:
src struts.xml com hellostruts2 action UploadFileAction.java WebContent upload error.jsp success.jsp upload.jsp WEB-INF web.xml lib commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang-2.4.jar commons-lang3-3.2.jar commons-logging-1.1.3.jar freemarker-2.3.22.jar javassist-3.11.0.GA.jar ognl-3.0.14.jar struts2-core-2.3.28.1.jar xwork-core-2.3.28.1.jar
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2CleanupFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter> <filter-name>struts2Filter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2CleanupFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <filter-mapping> <filter-name>struts2Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.multipart.maxSize" value="1000000" /> <package name="uploadstruts" extends="struts-default"> <action name="upload" class="com.hellostruts2.action.UploadFileAction" method="execute"> <result name="success">/upload/success.jsp</result> <result name="error">/upload/error.jsp</result> </action> </package> </struts>
UploadFileAction.Java
package com.hellostruts2.action; import java.io.*; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class UploadFileAction extends ActionSupport { private static final long serialVersionUID = 1L; private File myFile; private String myFileContentType; private String myFileFileName; private String destPath; public String execute(){ /* Copy file to a safe location */ destPath = "C:/apache-tomcat-8.0.23/work/"; try{ System.out.println("Src File name: " + myFile); System.out.println("Dst File name: " + myFileFileName); File destFile = new File(destPath, myFileFileName); FileUtils.copyFile(myFile, destFile); }catch(IOException e){ e.printStackTrace(); return ERROR; } return SUCCESS; } public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } }