Struts2多文件上传

Struts2多文件上传

 1 package com.tcf.action;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 public class UploadesAction extends ActionSupport {
12     private File[] file;
13     private String[] fileContentType;
14     private String[] fileFileName;
15     private String savePath;
16     public File[] getFile() {
17         return file;
18     }
19 
20     public void setFile(File[] file) {
21         this.file = file;
22     }
23 
24     public String[] getFileContentType() {
25         return fileContentType;
26     }
27 
28     public void setFileContentType(String[] fileContentType) {
29         this.fileContentType = fileContentType;
30     }
31 
32     public String[] getFileFileName() {
33         return fileFileName;
34     }
35 
36     public void setFileFileName(String[] fileFileName) {
37         this.fileFileName = fileFileName;
38     }
39 
40     public String getSavePath() {
41         return savePath;
42     }
43 
44     public void setSavePath(String savePath) {
45         this.savePath = savePath;
46     }
47 
48     public String upload(){
49         byte[] buf = new byte[1024];
50         try {
51             for(int i = 0;i<file.length;i++){
52                 FileInputStream fis = new FileInputStream(file[i]);//
53                 FileOutputStream fos = new FileOutputStream(savePath+"/"+fileFileName[i]);//放到哪里
54                 int len = 0;
55                 while((len=fis.read(buf))!=-1){
56                     fos.write(buf,0,len);
57                 }
58                 //刷新缓存
59                 //fos.flush();
60                 fos.close();
61                 fis.close();
62             }
63         } catch (FileNotFoundException e) {
64             // TODO Auto-generated catch block
65             e.printStackTrace();
66         } catch (IOException e) {
67             // TODO Auto-generated catch block
68             e.printStackTrace();
69         }
70         return SUCCESS;
71     }
72 }

struts.xml

1 <action name="uploades" class="com.tcf.action.UploadesAction" method="upload">
2     <param name="savePath">E:/temp</param>
3     <result>uploadSuccess.jsp</result>
4 </action>

upload.jsp

1 <s:form action="uploades.action" enctype="multipart/form-data" method="post">
2         <s:file name="file" />
3         <s:file name="file" />
4         <s:file name="file" />
5         <s:submit></s:submit>
6 </s:form>

 

posted @ 2016-09-23 13:12  云苇  阅读(116)  评论(0编辑  收藏  举报