1、首先得配置一下Struts得配置文件struts-xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 4 "http://struts.apache.org/dtds/struts-2.1.7.dtd"> 5 6 <struts> 7 <!-- 设置可否动态调用方法 --> 8 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 9 10 <!-- 设置访问路径的后缀名 --> 11 <constant name="struts.action.extension" value="do,action"/> 12 13 <!-- 设置上传文件的最大值 10M左右--> 14 <constant name="struts.multipart.maxSize" value="10701096"/> 15 16 <!-- 17 上传单个文件 18 --> 19 <package name="uploadfile" namespace="/uploadfile" extends="struts-default"> 20 <action name="uploadfile" class="it.web.action.UploadFileAction" method="execute"> 21 <result name="success">/WEB-INF/demo/uploadfile.jsp</result> 22 </action> 23 </package> 24 25 <!-- 26 上传多个文件 27 --> 28 <package name="uploadfiles" namespace="/uploadfiles" extends="struts-default"> 29 <action name="uploadfiles" class="it.web.action.UploadFileAction" method="execute"> 30 <result name="success">/WEB-INF/demo/uploadfiles.jsp</result> 31 </action> 32 </package> 33 34 </struts>
2、对应的action类为:
1 package it.web.action;
2
3 import java.io.File;
4 import org.apache.commons.io.FileUtils;
5 import org.apache.struts2.ServletActionContext;
6
7 import com.opensymphony.xwork2.ActionContext;
8
9 public class UploadFileAction {
10 //上传单个文件字段
11 private File image; //文件(必须要和表单的name属性值一样)
12 private String imageFileName; //文件名称(表单的name属性值+FileName)
13 private String imageContentType;//得到上传文件的类型(表单的name属性值+ContentType)
14
15
16 //上传多个文件的字段
17 private File[] images;
18 private String[] imageFileNames;
19 private String[] imagesContentType;
20 public File getImage() {
21 return image;
22 }
23 public void setImage(File image) {
24 this.image = image;
25 }
26
27 public String getImageFileName() {
28 return imageFileName;
29 }
30 public void setImageFileName(String imageFileName) {
31 this.imageFileName = imageFileName;
32 }
33
34 public String getImageContentType() {
35 return imageContentType;
36 }
37 public void setImageContentType(String imageContentType) {
38 this.imageContentType = imageContentType;
39 }
40
41 /*************************************************************************/
42 public File[] getImages() {
43 return images;
44 }
45 public void setImages(File[] images) {
46 this.images = images;
47 }
48
49 public String[] getImageFileNames() {
50 return imageFileNames;
51 }
52 public void setImageFileNames(String[] imageFileNames) {
53 this.imageFileNames = imageFileNames;
54 }
55
56 public String[] getImagesContentType() {
57 return imagesContentType;
58 }
59 public void setImagesContentType(String[] imagesContentType) {
60 this.imagesContentType = imagesContentType;
61 }
62
63
64
65 /*
66 * 上传单个文件
67 */
68 public String execute() throws Exception{
69 //image
70 /*
71 * 得到上传文件的真实路径
72 */
73 String realpath = ServletActionContext.getServletContext().getRealPath("/image");
74 System.out.println(realpath);
75
76 //判断文件是否存在
77 if(image!=null){
78 /*
79 * 创建文件虚拟的
80 *
81 * new File(realpath):文件路径
82 *
83 * imageFileName:文件名称
84 *
85 */
86 File savefile = new File(new File(realpath), imageFileName);
87
88 if(!savefile.getParentFile().exists())
89 {
90 savefile.getParentFile().mkdir();
91
92 FileUtils.copyFile(image, savefile);
93 ActionContext.getContext().put("message", "文件上传成功");
94 }
95 }
96 return "success";
97 }
98
99 /*
100 * 上传多个文件
101 */
102 public String execute1() throws Exception{
103 //image
104 /*
105 * 得到上传文件的真实路径(需要保存的路径)
106 */
107 String realpath = ServletActionContext.getServletContext().getRealPath("/image");
108 System.out.println(realpath);
109
110 //判断文件是否存在
111 if(images!=null){
112 /*
113 * 创建文件虚拟的
114 *
115 * new File(realpath):文件路径
116 *
117 * imageFileName:文件名称
118 *
119 */
120 File savedir = new File(realpath);
121
122 if(!savedir.getParentFile().exists())
123 {
124 savedir.mkdirs();
125
126 for(int i=0;i<images.length;i++)
127 {
128 File savefile = new File(savedir,imageFileNames[i]);
129 FileUtils.copyFile(images[i], savefile);
130 }
131 ActionContext.getContext().put("message", "多个文件文件上传成功");
132 }
133 }
134 return "success";
135 }
136 }
3、对应的上传单个文件的jsp页面为:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="${pageContext.request.contextPath}/uploadfile/uploadfile" enctype="multipart/form-data" method="post">
文件:<input type="file" name="image"/>
<input type="submit" value="上传"/><br>
文件类型:${imageContentType}
文件上传状态:${message}
</form>
</body>
</html>
4、对应的上传多个文件的jsp页面为:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="${pageContext.request.contextPath}/uploadfiles/uploadfiles" enctype="multipart/form-data" method="post">
文件:<input type="file" name="images"/><br>
<input type="file" name="images"/><br>
<input type="file" name="images"/>
<input type="submit" value="上传"/><br>
文件上传状态:${message}
</form>
</body>
</html>
注:项目中用到的一些jar包如下: