码迷,mamicode.com
首页 > 其他好文 > 详细

struts2中文件上传

时间:2014-06-06 17:36:01      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

注意点

     private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
     private String imageFileName;//   上传输入域FileName  文件名
     private String imageContentType;// 上传文件的MIME类型

单个文件

bubuko.com,布布扣
 1 package cn.itcast.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.InputStream;
 8 import java.io.OutputStream;
 9 import java.io.Serializable;
10 
11 import javax.servlet.ServletContext;
12 
13 import org.apache.commons.io.FileUtils;
14 import org.apache.struts2.ServletActionContext;
15 
16 import com.opensymphony.xwork2.ActionContext;
17 import com.opensymphony.xwork2.ActionSupport;
18 
19 public class UploadAction1 extends ActionSupport implements Serializable {
20     
21     private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
22     private String imageFileName;//   上传输入域FileName  文件名
23     private String imageContentType;// 上传文件的MIME类型
24     
25     public File getImage() {
26         return image;
27     }
28 
29 
30 
31     public void setImage(File image) {
32         this.image = image;
33     }
34 
35 
36 
37     public String getImageFileName() {
38         return imageFileName;
39     }
40 
41 
42 
43     public void setImageFileName(String imageFileName) {
44         this.imageFileName = imageFileName;
45     }
46 
47 
48 
49     public String getImageContentType() {
50         return imageContentType;
51     }
52 
53 
54 
55     public void setImageContentType(String imageContentType) {
56         this.imageContentType = imageContentType;
57     }
58 
59 
60 
61     public String execute(){
62         System.out.println(imageContentType);
63         try {
64             //处理实际的上传代码
65             //找到存储文件的真实路径
66 //        System.out.println(imageFileName);
67             ServletContext sc = ServletActionContext.getServletContext();
68             String storePath = sc.getRealPath("/files");
69             //构建输入输出流
70 //            OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);
71 //            InputStream in = new FileInputStream(image);
72 //            byte b[] = new byte[1024];
73 //            int len = -1;
74 //            while((len=in.read(b))!=-1){
75 //                out.write(b, 0, len);
76 //            }
77 //            out.close();
78 //            in.close();
79             
80             FileUtils.copyFile(image, new File(storePath,imageFileName));
81             
82             ActionContext.getContext().put("message", "上传成功!");
83             return SUCCESS;
84         } catch (Exception e) {
85             e.printStackTrace();
86             return ERROR;
87         }
88     }
89 }
bubuko.com,布布扣

jsp中

bubuko.com,布布扣
1   <body>
2     <form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">
3         文件:<input type="file" name="image"/><br/>
4         <input type="submit" value="上传"/>
5     </form>
6   </body>
bubuko.com,布布扣

 

 

 

多个文件上传

bubuko.com,布布扣
 1 package cn.itcast.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.InputStream;
 8 import java.io.OutputStream;
 9 import java.io.Serializable;
10 
11 import javax.servlet.ServletContext;
12 
13 import org.apache.commons.io.FileUtils;
14 import org.apache.struts2.ServletActionContext;
15 
16 import com.opensymphony.xwork2.ActionContext;
17 import com.opensymphony.xwork2.ActionSupport;
18 
19 public class UploadAction2 extends ActionSupport implements Serializable {
20     
21     private File[] images;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
22     private String[] imagesFileName;//   上传输入域FileName  文件名
23     private String[] imagesContentType;// 上传文件的MIME类型
24     
25     
26 
27     public File[] getImages() {
28         return images;
29     }
30 
31 
32 
33     public void setImages(File[] images) {
34         this.images = images;
35     }
36 
37 
38 
39     public String[] getImagesFileName() {
40         return imagesFileName;
41     }
42 
43 
44 
45     public void setImagesFileName(String[] imagesFileName) {
46         this.imagesFileName = imagesFileName;
47     }
48 
49 
50 
51     public String[] getImagesContentType() {
52         return imagesContentType;
53     }
54 
55 
56 
57     public void setImagesContentType(String[] imagesContentType) {
58         this.imagesContentType = imagesContentType;
59     }
60 
61 
62 
63     public String execute(){
64         try {
65             
66             if(images!=null&&images.length>0){
67                 ServletContext sc = ServletActionContext.getServletContext();
68                 String storePath = sc.getRealPath("/files");
69                 for(int i=0;i<images.length;i++)
70                     FileUtils.copyFile(images[i], new File(storePath,imagesFileName[i]));
71             }
72             ActionContext.getContext().put("message", "上传成功!");
73             return SUCCESS;
74         } catch (Exception e) {
75             e.printStackTrace();
76             return ERROR;
77         }
78     }
79 }
bubuko.com,布布扣

jsp中

bubuko.com,布布扣
1   <body>
2     <form action="${pageContext.request.contextPath}/upload/upload2.action" method="post" enctype="multipart/form-data">
3         文件1:<input type="file" name="images"/><br/>
4         文件2:<input type="file" name="images"/><br/>
5         <input type="submit" value="上传"/>
6     </form>
7   </body>
bubuko.com,布布扣

struts.xml中配置

设置文件上传大小

bubuko.com,布布扣
1     <constant name="struts.multipart.maxSize" value="52428800"></constant>
bubuko.com,布布扣
bubuko.com,布布扣
1     <package name="upload" namespace="/upload" extends="mypackage">
2         <action name="upload1" class="cn.itcast.action.UploadAction1" method="execute">
3             <result name="success">/success.jsp</result>
4         </action>
5         <action name="upload2" class="cn.itcast.action.UploadAction2" method="execute">
6             <result name="success">/success.jsp</result>
7         </action>
8     </package>
bubuko.com,布布扣

 

struts2中文件上传,布布扣,bubuko.com

struts2中文件上传

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/friends-wf/p/3766374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!