1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib uri="/struts-tags" prefix="s" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     <s:form action="upload.action" method="post" enctype="multipart/form-data">
12         <s:textfield name="username" label="用户名"/>
13         <s:file name="filename" label="请选择文件"/>
14         <s:submit value="提交"/>
15     </s:form>
16 </body>
17 </html>
index.jsp代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>struts2_1</display-name>
 4   <filter>
 5       <filter-name>struts2</filter-name>
 6       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 7   </filter>
 8   <filter-mapping>
 9       <filter-name>struts2</filter-name>
10       <url-pattern>/*</url-pattern>
11   </filter-mapping>
12   <welcome-file-list>
13     <welcome-file>index.jsp</welcome-file>
14   </welcome-file-list>
15 </web-app>
web.xml代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.devMode" value="true" />
 7     <package name="hello" extends="struts-default" namespace="/">
 8         <action name="upload" class="com.xiaostudy.web.Upload" method="upload"></action>
 9     </package>
10 </struts>
struts.xml代码
 1 package com.xiaostudy.web;
 2 
 3 import java.io.File;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 public class Upload extends ActionSupport{
10     
11     public String username;
12     public File filename;
13     public String filenameFileName;
14     public String filenameContentType;
15     
16     public String upload() {
17         
18         String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");
19         File file = new File(path);
20         if(!file.exists()) {
21             file.mkdirs();
22         }
23         filename.renameTo(new File(file, filenameFileName));
24         
25         return NONE;
26     }
27 
28     public String getUsername() {
29         return username;
30     }
31 
32     public void setUsername(String username) {
33         this.username = username;
34     }
35 
36     public File getFilename() {
37         return filename;
38     }
39 
40     public void setFilename(File filename) {
41         this.filename = filename;
42     }
43 
44     public String getFilenameFileName() {
45         return filenameFileName;
46     }
47 
48     public void setFilenameFileName(String filenameFileName) {
49         this.filenameFileName = filenameFileName;
50     }
51 
52     public String getFilenameContentType() {
53         return filenameContentType;
54     }
55 
56     public void setFilenameContentType(String filenameContentType) {
57         this.filenameContentType = filenameContentType;
58     }
59 
60 
61 }
action动作类Upload