1 import java.io.File;
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.io.Serializable;
5
6 import org.apache.commons.io.FileUtils;
7 import org.apache.struts2.ServletActionContext;
8
9 import com.opensymphony.xwork2.ActionContext;
10 import com.opensymphony.xwork2.ActionSupport;
11
12 public class FileUpload extends ActionSupport {
13
14 private File[] file;
15 private String[] fileFileName;
16 private String[] filetype;
17
18 /**
19 * 批量上传文件方法
20 * @return
21 * @throws IOException
22 */
23 public String upload()
24 throws IOException {
25 String root=ServletActionContext.getServletContext().getRealPath("/upload");
26 if (file!=null && file.length>0) {
27 for (int j = 0; j < file.length; j++) {
28 File myfile=new File(root,fileFileName[j]);
29 FileUtils.copyFile(file[j], myfile);
30 }
31 return "upload";
32 }else {
33 return "failupload";
34 }
35 }
36
37
38 public File[] getFile() {
39 return file;
40 }
41 public void setFile(File[] file) {
42 this.file = file;
43 }
44 public String[] getFiletype() {
45 return filetype;
46 }
47 public void setFiletype(String[] filetype) {
48 this.filetype = filetype;
49 }
50
51 public String[] getFileFileName() {
52 return fileFileName;
53 }
54
55 public void setFileFileName(String[] fileFileName) {
56 this.fileFileName = fileFileName;
57 }
58
59
60 }
1 <constant name="struts.serve.static.browserCache" value="true" />
2 <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
3 <constant name="struts.multipart.maxSize" value="10701096"/>
4 <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->
5 <constant name="struts.multipart.saveDir " value="d:/tmp" />
6
7 <package name="fileaction" namespace="/" extends="struts-default">
8 <action name="file_*" class="com.dkx.action.FileUpload" method="{1}">
9 <result name="upload">/index.jsp</result>
10 <result name="failupload">/index.jsp</result>
11
12 </action>
13 </package>
View Code
1 <form action="${basePath }file_upload.action" enctype="multipart/form-data" method="post">
2 <input type="file" name="file"><br>
3 <input type="file" name="file"><input type="submit" value="提交">
4 </form>
View Code