以下内容是基于导入struts2-2.3.32.jar包来讲的
1.文件上传
A.单文件上传
<body> <form action="${pageContext.request.contextPath }/one" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="上传"/> </form> </body>
1 package com.rong.web.action;
2
3 import java.io.File;
4
5 import org.apache.commons.io.FileUtils;
6
7 import com.opensymphony.xwork2.ActionSupport;
8
9 public class MyAction extends ActionSupport {
10 private static final long serialVersionUID = -351587239525292420L;
11 //保存上传的文件对象,file对应表单元素名称,名称必须一致,拦截器会解析这个格式!
12 private File file;
13 //文件名,必须为fileFileName
14 private String fileFileName;
15 //文件类型,必须为fileContentType
16 private String fileContentType;
17 public File getFile() {
18 return file;
19 }
20 public void setFile(File file) {
21 this.file = file;
22 }
23
24 public String getFileFileName() {
25 return fileFileName;
26 }
27 public void setFileFileName(String fileFileName) {
28 this.fileFileName = fileFileName;
29 }
30 public String getFileContentType() {
31 return fileContentType;
32 }
33 public void setFileContentType(String fileContentType) {
34 this.fileContentType = fileContentType;
35 }
36 @Override
37 public String execute() throws Exception {
38 //E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\ upload_1742945b_24ed_4612_a2f4_b83cffa61620_00000000.tmp
39 //struts会保存到Tomcat服务器的struts2文件夹中创建临时文件,若不处理,执行完代码会把此临时文件删除
40 System.out.println(file.getAbsolutePath());
41 //butterfly.jpg
42 System.out.println(fileFileName);
43 //image/png
44 System.out.println(fileContentType);
45 File targetFile=new File("c:/", fileFileName);
46 FileUtils.copyFile(file, targetFile);
47 return SUCCESS;
48 }
49 }
文件类型: mime-type想了解的可以去Tomcat服务器的web.xml文件查看,里面有文件类型配置
E:\apache-tomcat-7.0.82\conf\web.xml