Struts 2控制多文件上传

在前面我已经介绍过了利用Struts 2控制单文件上传:Struts 2读书笔记-----使用Struts 2控制文件上传 .其实对于多文件上传也差不多。加入我们要同时控制三个文件进行上传。那么页面得有三个文件上传域。在这里主要介绍采用数字和list来控制文件上传。

 

        一、利用数组

         利用数组来封装3个文件域。为了让数组一次性封装三个文件,我们需要将三个文件域的name属性设置为一样的。如下:

1.  <form action="upload.action" method="POST" enctype="multipart/form-data">  
2.      文件标题:<input type="text" name="title"/><Br />  
3.      第一个文件:<input type="file" name="upload"/><br/>  
4.      第二个文件:<input type="file" name="upload"/><br/>  
5.      第三个文件:<input type="file" name="upload"/><br/>  
6.      <input type="submit" value="上传"/>  
7.  </form>  


       在处理上传的过程中,我们需要3个数组来分别封装文件名、文件类型、文件内容

     

1.  public class UploadToArrayAction extends ActionSupport {  
2.      private String title;                          // 文件标题  
3.      private File[] upload;                         // 使用file数组封装多个文件域对应的文件内容  
4.      private String[] uploadContentType;             // 使用字符串数组封装多个文件域对应的文件类型  
5.      private String[] uploadFileName;               // 使用字符串数组封装对个文件域对应的文件名  
6.    
7.      private String savePath;                       // 动态设置文件属性  
8.    
9.      public String getTitle() {  
10.         return title;                          
11.     }  
12.   
13.     public void setTitle(String title) {  
14.         this.title = title;  
15.     }  
16.   
17.     public File[] getUpload() {  
18.         return upload;  
19.     }  
20.   
21.     public void setUpload(File[] upload) {  
22.         this.upload = upload;  
23.     }  
24.   
25.     public String[] getUploadContentType() {  
26.         return uploadContentType;  
27.     }  
28.   
29.     public void setUploadContentType(String[] uploadContentType) {  
30.         this.uploadContentType = uploadContentType;  
31.     }  
32.   
33.     public String[] getUploadFileName() {  
34.         return uploadFileName;  
35.     }  
36.   
37.     public void setUploadFileName(String[] uploadFileName) {  
38.         this.uploadFileName = uploadFileName;  
39.     }  
40.   
41.     public String getSavePath() {  
42.         return savePath;  
43.     }  
44.   
45.     public void setSavePath(String savePath) {     
46.         this.savePath = savePath;  
47.     }  
48.   
49.     public String execute() throws Exception{  
50.         //取得需要上传的文件数组  
51.         File[] files = getUpload();  
52.         //遍历每个需要上传的文件  
53.         for (int i = 0; i < files.length; i++) {  
54.             //以服务器的文件保存地址和源文件名建立上传文件输出流  
55.             String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");  
56.          FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName()[i]);  
57.          //以每个需要上传的文件建立输入流  
58.          FileInputStream fis = new FileInputStream(files[i]);  
59.          //将每个需要上传的文件写入  
60.          byte[] buffer = new byte[1024];  
61.          int length = 0;  
62.          while ((length=fis.read(buffer))>0) {  
63.              fos.write(buffer,0,length);  
64.          }  
65.         fos.close();  
66.     }  
67.     return SUCCESS;  
68. }  


   

          struts.xml配置文件的配置

1.  <package name="mystruts" extends="struts-default">  
2.          <action name="upload" class="com.app.action.UploadToArrayAction">  
3.              <!-- 动态设置Action的savePath属性 -->  
4.              <param name="savePath">/upload</param>  
5.              <!-- 配置文件上传的拦截器 -->                         
6.              <interceptor-ref name="fileUpload">  
7.                  <!-- 配置允许上传的文件类型 -->  
8.                  <param name="allowedTypes">image/png,image/gif,image/jpeg</param>  
9.                  <param name="maximumSize">20480000</param>  
10.             </interceptor-ref>  
11.               
12.             <!-- 配置系统默认的拦截器 -->  
13.             <interceptor-ref name="defaultStack"></interceptor-ref>  
14.             <result name="success">/success.jsp</result>  
15.             <result name="input">/upload.jsp</result>  
16.         </action>  
17.     </package>  



         配置这个Action与配置单文件上传的Action其实是没有什么其他的区别的。

         

         二、利用List

          其实利用List和利用数组时没有什么区别的。只是将Array数组改为了泛型了list而已了。

1.  public class UploadToListAction extends ActionSupport {  
2.      private String title; // 文件标题  
3.      private List<File> upload; // 使用file数组封装多个文件域对应的文件内容  
4.      private List<String> uploadContentType; // 使用字符串数组封装多个文件域对应的文件类型  
5.      private List<String> uploadFileName; // 使用字符串数组封装对个文件域对应的文件名  
6.    
7.      private String savePath; // 动态设置文件属性  
8.    
9.      public String getTitle() {  
10.         return title;  
11.     }  
12.   
13.     public void setTitle(String title) {  
14.         this.title = title;  
15.     }  
16.   
17.     public List<File> getUpload() {  
18.         return upload;  
19.     }  
20.   
21.     public void setUpload(List<File> upload) {  
22.         this.upload = upload;  
23.     }  
24.   
25.     public List<String> getUploadContentType() {  
26.         return uploadContentType;  
27.     }  
28.   
29.     public void setUploadContentType(List<String> uploadContentType) {  
30.         this.uploadContentType = uploadContentType;  
31.     }  
32.   
33.     public List<String> getUploadFileName() {  
34.         return uploadFileName;  
35.     }  
36.   
37.     public void setUploadFileName(List<String> uploadFileName) {  
38.         this.uploadFileName = uploadFileName;  
39.     }  
40.   
41.     public String getSavePath() {  
42.         return savePath;  
43.     }  
44.   
45.     public void setSavePath(String savePath) {  
46.         this.savePath = savePath;  
47.     }  
48.   
49.     public String execute() throws Exception {  
50.         // 取得需要上传的文件数组  
51.         List<File> files = getUpload();  
52.         // 遍历每个需要上传的文件  
53.         for (int i = 0; i < files.size(); i++) {  
54.             String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");  
55.             FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName().get(i));  
56.             FileInputStream fis = new FileInputStream(files.get(i));  
57.             byte[] buffer = new byte[1024];  
58.             int length = 0;  
59.             while((length=fis.read())>0){  
60.                 fos.write(buffer,0,length);  
61.             }  
62.             fos.close();  
63.         }  
64.         return SUCCESS;  
65.     }  
66. }  


 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值