码迷,mamicode.com
首页 > Web开发 > 详细

Struts2 文件上传

时间:2015-07-11 16:37:40      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

文件上传

界面

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
 2 <%
 3     String homePage = request.getContextPath();
 4 %>
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8     <title>文件上传</title>
 9 </head>
10 <body>
11     <form action="<%=homePage%>/updateAndDownload/upLoadDo.action" method="post" enctype="multipart/form-data">
12         <table>
13             <caption>文件上传</caption>
14             <tr>
15                 <td>文件标题</td>
16                 <td>
17                     <input type="text" name="title" />
18                 </td>
19             </tr>
20             <tr>
21                 <td>选择文件</td>
22                 <td>
23                     <input type="file" name="upload" />
24                 </td>
25             </tr>
26             <tr>
27                 <td colspan="2" align="right">
28                     <input type="submit" value="上传" />
29                 </td>
30             </tr>
31         </table>
32     </form>
33 </body>
34 </html>

处理上传的Action

 1 package org.zln.updateAndDownload.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import org.apache.struts2.ServletActionContext;
 5 
 6 import java.io.*;
 7 import java.util.UUID;
 8 
 9 /**
10  * Created by sherry on 000010/7/10 9:52.
11  */
12 public class FileUpAndDownload extends ActionSupport {
13 
14     private String title;
15 
16     /*上传文件*/
17     private File upload;
18     /*文件类型*/
19     private String uploadContentType;
20     /*文件名*/
21     private String uploadFileName;
22 
23     /*保存路径*/
24     private String savePath;
25 
26     public String upLoadUI(){
27         return SUCCESS;
28     }
29 
30     public String upLoadDo() throws IOException {
31         /**/
32         File outWrite = new File(getSavePath());
33         if (!outWrite.exists()){
34             outWrite.mkdir();
35         }
36         FileOutputStream fileOutputStream = new FileOutputStream(getSavePath()+File.separatorChar+ UUID.randomUUID()+getUploadFileName());
37         /**/
38         FileInputStream fileInputStream = new FileInputStream(upload);
39 
40         byte[] bytes = new byte[1024];
41         int len = 0;
42         while ((len = fileInputStream.read(bytes)) > 0){
43             fileOutputStream.write(bytes,0,len);
44         }
45         fileOutputStream.close();
46         fileInputStream.close();
47         return SUCCESS;
48     }
49 
50     public String getTitle() {
51         return title;
52     }
53 
54     public void setTitle(String title) {
55         this.title = title;
56     }
57 
58     public File getUpload() {
59         return upload;
60     }
61 
62     public void setUpload(File upload) {
63         this.upload = upload;
64     }
65 
66     public String getUploadContentType() {
67         return uploadContentType;
68     }
69 
70     public void setUploadContentType(String uploadContentType) {
71         this.uploadContentType = uploadContentType;
72     }
73 
74     public String getUploadFileName() {
75         return uploadFileName;
76     }
77 
78     public void setUploadFileName(String uploadFileName) {
79         this.uploadFileName = uploadFileName;
80     }
81 
82     //返回文件保存路径
83     public String getSavePath() {
84         return ServletActionContext.getServletContext().getRealPath(savePath);
85     }
86 
87     public void setSavePath(String savePath) {
88         this.savePath = savePath;
89     }
90 }

配置

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8     <!--必须继承default才能使用其全局跳转页面-->
 9     <package name="updateAndDownload" namespace="/updateAndDownload" extends="default">
10         <!--文件上传-->
11         <action name="upLoadUI" class="org.zln.updateAndDownload.action.FileUpAndDownload" method="upLoadUI">
12             <result>/WEB-INF/updateAndDownload/upLoadUI.jsp</result>
13         </action>
14         <action name="upLoadDo" class="org.zln.updateAndDownload.action.FileUpAndDownload" method="upLoadDo">
15             <param name="savePath">/upload</param>
16             <result name="success">/WEB-INF/updateAndDownload/upLoadSucc.jsp</result>
17             <result name="input">/WEB-INF/updateAndDownload/upLoadUI.jsp</result>
18         </action>
19     </package>
20 
21 </struts>
<constant name="struts.multipart.maxSize" value="1000000000"/>

 

如何上传多个文件?

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
 2 <%
 3     String homePage = request.getContextPath();
 4 %>
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8     <title>文件上传</title>
 9 </head>
10 <body>
11     <form action="<%=homePage%>/updateAndDownload/upLoadDo.action" method="post" enctype="multipart/form-data">
12         <table>
13             <caption>文件上传</caption>
14             <tr>
15                 <td>文件标题</td>
16                 <td>
17                     <input type="text" name="title" />
18                 </td>
19             </tr>
20             <tr>
21                 <td>选择文件</td>
22                 <td>
23                     <input type="file" name="upload" />
24                 </td>
25             </tr>
26             <tr>
27                 <td>选择文件</td>
28                 <td>
29                     <input type="file" name="upload" />
30                 </td>
31             </tr>
32             <tr>
33                 <td>选择文件</td>
34                 <td>
35                     <input type="file" name="upload" />
36                 </td>
37             </tr>
38             <tr>
39                 <td colspan="2" align="right">
40                     <input type="submit" value="上传" />
41                 </td>
42             </tr>
43         </table>
44     </form>
45     <hr/>
46     <a href="<%=homePage%>/updateAndDownload/showUploadFiles.action">显示已上传文件</a><br/>
47 </body>
48 </html>
  1 package org.zln.updateAndDownload.action;
  2 
  3 import com.opensymphony.xwork2.ActionContext;
  4 import com.opensymphony.xwork2.ActionSupport;
  5 import org.apache.struts2.ServletActionContext;
  6 
  7 import java.io.*;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 import java.util.UUID;
 11 
 12 /**
 13  * Created by sherry on 000010/7/10 9:52.
 14  */
 15 public class FileUpAndDownload extends ActionSupport {
 16 
 17     private String title;
 18 
 19     /*上传文件*/
 20     private File[] upload;
 21     /*文件类型*/
 22     private String[] uploadContentType;
 23     /*文件名*/
 24     private String[] uploadFileName;
 25 
 26     /*保存路径*/
 27     private String savePath;
 28 
 29     //文件上传界面
 30     public String upLoadUI(){
 31         return SUCCESS;
 32     }
 33 
 34     //文件上传动作
 35     public String upLoadDo() throws IOException {
 36         /**/
 37         File outWrite = new File(getSavePath());
 38         if (!outWrite.exists()){
 39             outWrite.mkdir();
 40         }
 41         BufferedInputStream bufferedInputStream = null;
 42         BufferedOutputStream bufferedOutputStream = null;
 43         for (int i = 0;i < upload.length;i++){
 44             bufferedInputStream = new BufferedInputStream(new FileInputStream(upload[i]));
 45             bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(getSavePath()+File.separatorChar+UUID.randomUUID()+uploadFileName[i]));
 46             byte[] bytes = new byte[4096];
 47             int len = 0;
 48             while ((len = bufferedInputStream.read(bytes))>0){
 49                 bufferedOutputStream.write(bytes,0,len);
 50             }
 51             bufferedOutputStream.close();
 52             bufferedInputStream.close();
 53         }
 54         return SUCCESS;
 55     }
 56 
 57     //显示上传的文件
 58     public String showUploadFiles(){
 59 
 60         File rootFile = new File(getSavePath());
 61         File[] files = rootFile.listFiles();
 62         List<String> listFile = new ArrayList<>();
 63         for (int i = 0; i < files.length; i++) {
 64             listFile.add(files[i].getAbsolutePath());
 65         }
 66         ActionContext.getContext().put("listFile",listFile);
 67         return SUCCESS;
 68     }
 69 
 70     //删除所有上传文件
 71     public String deleteAllUploadFiles(){
 72         File rootFile = new File(getSavePath());
 73         File[] files = rootFile.listFiles();
 74         for (File file:files){
 75             file.delete();
 76         }
 77         return SUCCESS;
 78     }
 79 
 80 
 81 
 82     public String getTitle() {
 83         return title;
 84     }
 85 
 86     public void setTitle(String title) {
 87         this.title = title;
 88     }
 89 
 90     public File[] getUpload() {
 91         return upload;
 92     }
 93 
 94     public void setUpload(File[] upload) {
 95         this.upload = upload;
 96     }
 97 
 98     public String[] getUploadContentType() {
 99         return uploadContentType;
100     }
101 
102     public void setUploadContentType(String[] uploadContentType) {
103         this.uploadContentType = uploadContentType;
104     }
105 
106     public String[] getUploadFileName() {
107         return uploadFileName;
108     }
109 
110     public void setUploadFileName(String[] uploadFileName) {
111         this.uploadFileName = uploadFileName;
112     }
113 
114     //返回文件保存路径
115     public String getSavePath() {
116         return ServletActionContext.getServletContext().getRealPath(savePath);
117     }
118 
119     public void setSavePath(String savePath) {
120         this.savePath = savePath;
121     }
122 }

使用拦截器控制允许上传的文件类型

 1         <action name="upLoadDo" class="org.zln.updateAndDownload.action.FileUpAndDownload" method="upLoadDo">
 2             <interceptor-ref name="fileUpload">
 3                 <param name="allowedTypes">
 4                     text/plain,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf
 5                 </param>
 6                 <param name="maximumSize">4000</param>
 7             </interceptor-ref>
 8             <interceptor-ref name="defaultStack"/>
 9             <param name="savePath">/upload</param>
10             <result name="success">/WEB-INF/updateAndDownload/upLoadSucc.jsp</result>
11             <result name="input">/WEB-INF/updateAndDownload/upLoadUI.jsp</result>
12         </action>

 

Struts2 文件上传

标签:

原文地址:http://www.cnblogs.com/sherrykid/p/4638781.html

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