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

struts2文件上传/文件下载

时间:2016-11-25 23:25:57      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:字节   tar   text   default   tco   类型   exist   ini   let   

文件上传

技术分享
 1 import java.io.File;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 
 7 public class Upload2Action extends ActionSupport {
 8 
 9     private String username;
10     private File[] photo;
11 
12     private String[] photoFileName;// 用来存储上传的文件名,命名规范:表单属性名+FileName
13     private String[] photoContentType;// 上传文件的MIME类型 jpg gif image/pjpeg
14                                         // image/gif
15 
16     public String getUsername() {
17         return username;
18     }
19 
20     public void setUsername(String username) {
21         this.username = username;
22     }
23 
24     public File[] getPhoto() {
25         return photo;
26     }
27 
28     public void setPhoto(File[] photo) {
29         this.photo = photo;
30     }
31 
32     public String[] getPhotoFileName() {
33         return photoFileName;
34     }
35 
36     public void setPhotoFileName(String[] photoFileName) {
37         this.photoFileName = photoFileName;
38     }
39 
40     public String[] getPhotoContentType() {
41         return photoContentType;
42     }
43 
44     public void setPhotoContentType(String[] photoContentType) {
45         this.photoContentType = photoContentType;
46     }
47 
48     public String upload() {
49 
50         // 创建服务器端存储文件的位置
51         String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
52         File dir = new File(path);
53         if (!dir.exists()) {
54             dir.mkdirs();
55         }
56 
57         // 把临时文件剪切到upload下
58         for (int i = 0; i < photo.length; i++) {
59             photo[i].renameTo(new File(dir, photoFileName[i]));
60 
61         }
62 
63         return "success";
64     }
65 
66 }
View Code

xml配置

技术分享
        <action name="upload" class="com.web.action.UploadAction" method="upload">
            <!-- 设置上传的文件的扩展名类型
            <interceptor-ref name="defaultStack">
                <param name="fileUpload.allowedExtensions">jpg,gif,png</param>
            </interceptor-ref>
             -->
             
             <!-- 设置上传文件的MIME类型 -->
             <interceptor-ref name="defaultStack">
                 <param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/png</param>
             </interceptor-ref>
             
            <result name="success">/success.jsp</result>
            <result name="input">/uplode.jsp</result>
        </action>
View Code

 

 文件下载

技术分享
 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.InputStream;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 //实现文件下载
10 public class DownLoadAcion extends ActionSupport {
11 
12     private InputStream inputStream;// 注意:该成员变量名不能叫in
13     private String filename;
14 
15     public String getFilename() {
16         return filename;
17     }
18 
19     public void setFilename(String filename) {
20         this.filename = filename;
21     }
22 
23     public InputStream getInputStream() {
24         return inputStream;
25     }
26 
27     public void setInputStream(InputStream inputStream) {
28         this.inputStream = inputStream;
29     }
30 
31     public String download() throws FileNotFoundException {
32 
33         // 设置被下载的文件的路径
34         String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload/struts-default.xml");
35 
36         // 创建读取被下载文件的字节读取流
37         inputStream = new FileInputStream(path);
38 
39         filename = "图片.jpg";
40 
41         // 接下来读取文件并写入到客户端不需要自己实现,都被结果类型stream实现了
42 
43         return "success";
44     }
45 
46 }
View Code

xml配置

技术分享
1 <action name="download" class="com.web.action.DownLoadAcion" method="download">
2             <result type="stream">
3                 <param name="inputName">inputStream</param>
4                       <param name="contentType">application/octet-stream</param>
5                       <!-- 通知浏览器去下载 -->
6                 <param name="contentDisposition">attachment;filename=${@java.net.URLEncoder@encode(filename,"UTF-8")}</param>
7             </result>
8         </action>
View Code

 

struts2文件上传/文件下载

标签:字节   tar   text   default   tco   类型   exist   ini   let   

原文地址:http://www.cnblogs.com/qzwbolgs/p/6103131.html

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