struts2文件上传和下载

Posted 光何

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2文件上传和下载相关的知识,希望对你有一定的参考价值。

1. struts系统中的拦截器介绍

过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截。

拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候

对方法的调用进行拦截,对拦截到的方法可以进行扩展,增强

Struts2中的拦截器主要针对Action中方法的调用进行拦截

 1 <interceptors>
 2  <!—声明n个拦截器-->
 3     <interceptor name="fileUpload"
 4         class="org.apache.struts2.interceptor.FileUploadInterceptor" />
 5     <interceptor name="i18n"
 6         class="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
 7     <interceptor name="logger"
 8         class="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
 9     <interceptor name="modelDriven"
10         class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
11     <interceptor name="params"
12         class="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
13     <interceptor name="staticParams"
14         class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
15     <interceptor name="token"
16         class="org.apache.struts2.interceptor.TokenInterceptor" />
17     <interceptor name="tokenSession"
18         class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
19 20 <!—由已经声明的n个拦截器,组成拦截器栈-->
21     <interceptor-stack name="defaultStack">
22         <interceptor-ref name="exception" />
23         <interceptor-ref name="alias" />
24         <interceptor-ref name="servletConfig" />
25         <interceptor-ref name="i18n" />
26         <interceptor-ref name="prepare" />
27         <interceptor-ref name="chain" />
28         <interceptor-ref name="scopedModelDriven" />
29         <interceptor-ref name="modelDriven" />
30         <interceptor-ref name="fileUpload" />
31         <interceptor-ref name="checkbox" />
32         <interceptor-ref name="multiselect" />
33         <interceptor-ref name="staticParams" />
34         <interceptor-ref name="actionMappingParams" />
35         <interceptor-ref name="params">
36             <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*
37             </param>
38         </interceptor-ref>
39         <interceptor-ref name="conversionError" />
40         <interceptor-ref name="validation">
41             <param name="excludeMethods">input,back,cancel,browse</param>
42         </interceptor-ref>
43         <interceptor-ref name="workflow">
44             <param name="excludeMethods">input,back,cancel,browse</param>
45         </interceptor-ref>
46         <interceptor-ref name="debugging" />
47         <interceptor-ref name="deprecation" />
48     </interceptor-stack>
49 </interceptors>
50 <default-interceptor-ref name="defaultStack" />
51 </package>
52  

拦截器栈:

 a --> b --> c --> execute() --> c --> b --> a

 

2.文件上传

2.1 Action

 1 /*** 处理文件上传的请求 **/
 2 public class FileAction
 3 {
 4     // org.apache.struts2.interceptor.FileUploadInterceptor
 5     // 1 定义文件上传需要的特定属性
 6     private File img; // 上传的文件对象
 7     private String imgContentType;// 删除的文件的内容的类型
 8     private String imgFileName;// 上传的文件名称
 9     private String path; // 文件保存到服务器上的目录
10 
11     public String getPath()
12     {
13         return path;
14     }
15 
16     public void setPath(String path)
17     {
18         this.path = path;
19     }
20 
21     public File getImg()
22     {
23         return img;
24     }
25 
26     public void setImg(File img)
27     {
28         this.img = img;
29     }
30 
31     public String getImgContentType()
32     {
33         return imgContentType;
34     }
35 
36     public void setImgContentType(String imgContentType)
37     {
38         this.imgContentType = imgContentType;
39     }
40 
41     public String getImgFileName()
42     {
43         return imgFileName;
44     }
45 
46     public void setImgFileName(String imgFileName)
47     {
48         this.imgFileName = imgFileName;
49     }
50 
51     /*** 处理文件上传的请求 ****/
52     public String uploadFile()
53     {
54         System.out.println(img);
55         System.out.println(imgContentType);
56         System.out.println(imgFileName);
57         // 修改文件名
58         String fname = UUID.randomUUID()
59                 + imgFileName.substring(imgFileName.lastIndexOf("."));
60         // 将上传的文件对象img保存到服务器的指定目录
61         // 1获得path的绝对路径
62         String pth = ServletActionContext.getServletContext().getRealPath(path);
63         // 2在pth下创建文件
64         File file = new File(pth, fname);
65         // 3将img内容拷贝file中
66         try
67         {
68             FileUtils.copyFile(img, file);
69         }
70         catch (IOException e)
71         {
72             e.printStackTrace();
73         }
74         return Action.SUCCESS;
75     }
76 }

2.2 struts.xml 配置

 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 <struts>
 6     <package name="file" namespace="/" extends="struts-default">
 7         <interceptors>
 8             <!-- 自定义拦截器栈 -->
 9             <interceptor-stack name="mystack">
10                 <!-- 引用系统文件上传的拦截器 -->
11                 <interceptor-ref name="fileUpload">
12                     <param name="allowedExtensions">txt,jpg,doc</param>
13                     <!-- 其他属性的赋值同理 -->
14                 </interceptor-ref>
15                 <!-- 引用系统基本拦截器栈 -->
16                 <interceptor-ref name="basicStack" />
17             </interceptor-stack>
18         </interceptors>
19         <action name="fileAction_*" class="com.bjsxt.action.FileAction"
20             method="{1}">
21             <!-- 引用自定义的拦截器栈 -->
22             <interceptor-ref name="mystack"></interceptor-ref>
23             <!-- 给path属性赋值 -->
24             <param name="path">filedir</param>
25             <result>/index.jsp</result>
26         </action>
27     </package>
28 </struts>

2.3实现UI页面 

1 <form action="fileAction_uploadFile.action" method="post"
2     enctype="multipart/form-data">
3     <input type="file" name="img" />
4     <input type="submit" />
5 </form>

3.文件下载

3.1 Action

 1 //org.apache.struts2.dispatcher.StreamResult
 2 public class DownloadAction
 3 {
 4     private InputStream inputStream;
 5     private String fname;// 需要下载的文件的名字
 6 
 7     public InputStream getInputStream()
 8     {
 9         System.out.println("----------getInputStream");
10         return inputStream;
11     }
12 
13     public String getFname()
14     {
15         return fname;
16     }
17 
18     public void setFname(String fname)
19     {
20         this.fname = fname;
21     }
22 
23     /*** 实现文件下载 ***/
24     public String download()
25     {
26         System.out.println("----------download");
27         // 获得附件的保存目录
28         String path = ServletActionContext.getServletContext().getRealPath(
29                 "filedir");
30         // 将下载的文件封装为InputStream的对象
31         try
32         {
33             inputStream = new FileInputStream(path + "/" + fname);
34         }
35         catch (FileNotFoundException e)
36         {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }
40         return Action.SUCCESS;
41     }
42 }

3.2 struts.xml 配置: 

 1 <package name="down" namespace="/down" extends="struts-default">
 2     <action name="downloadAction_*" class="com.bjsxt.action.DownloadAction"
 3         method="{1}">
 4         <result type="stream">
 5             <param name="inputName">inputStream</param>
 6             <!-- 指定下载的附件的类型 -->
 7             <param name="contentType">application/octet-stream</param>
 8             <!-- 指定下载的附件的名称 -->
 9             <param name="contentDisposition">attachment;filename=${fname}</param>
10         </result>
11     </action>
12 </package>

 

以上是关于struts2文件上传和下载的主要内容,如果未能解决你的问题,请参考以下文章

文件上传(多文件上传)/下载

struts2学习(14)struts2文件上传和下载多个文件上传和下载

struts2实现文件上传下载

struts2学习(13)struts2文件上传和下载

Struts2入门——Struts2的文件上传和下载

struts2文件上传下载