struts 文件下载 annotation 注解版

【本文简介】

本文将简单介绍使用 struts2 ,通过零配置和 annotation 实现文件下载功能。

【文件夹结构】

【web.xml有关struts的配置】

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>

【访问的对应的url】

以上的web.xml配置导致下面的访问地址的方法名有要加:.action

http://localhost:8080/TestBySSH/download!testDownload.action?fileName=file1.txt

【action代码】

 1 package com.modelsystem.action;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.InputStream;
 6 
 7 import org.apache.struts2.convention.annotation.Namespace;
 8 import org.apache.struts2.convention.annotation.ParentPackage;
 9 import org.apache.struts2.convention.annotation.Result;
10 import org.apache.struts2.convention.annotation.ResultPath;
11 import org.apache.struts2.convention.annotation.Results;
12  
13 
14 /**
15  * @描述 struts 文件下载 annotation 注解版
16  * @作者   小M
17  * @博客 http://www.cnblogs.com/xiaoMzjm/
18  * @时间 2014/07/30
19  */
20 @ParentPackage("struts-default")   
21 @Namespace("/")   
22 @ResultPath(value = "/")   
23 @Results({   
24         @Result(params = {
25                 // 下载的文件格式
26                 "contentType", "application/octet-stream",   
27                 // 调用action对应的方法
28                 "inputName", "inputStream",   
29                 // HTTP协议,使浏览器弹出下载窗口
30                 "contentDisposition", "attachment;filename="${fileName}"",   
31                 // 文件大小
32                 "bufferSize", "10240"},   
33                 // result 名
34                 name = "download", 
35                 // result 类型
36                 type = "stream")   
37 })  
38 public class DownloadAction extends BaseAction{
39      
40     private static final long serialVersionUID = 1L;
41  
42     /**  
43      * 下载文件名
44      * 对应annotation注解里面的${fileName},struts 会自动获取该fileName
45      */  
46     private String fileName;   
47    
48     public String getFileName() {   
49         return fileName;   
50     }   
51    
52     public void setFileName(String fileName) {   
53         this.fileName = fileName;   
54     }  
55      
56     /**  
57      * 下载文件应访问该地址
58      * 对应annotation注解里面的 name = "download"
59      */  
60     public String testDownload() {   
61         return "download";   
62     }   
63         
64     /**  
65      * 获取下载流
66      * 对应 annotation 注解里面的 "inputName", "inputStream"
67      * 假如 annotation 注解改为 "inputName", "myStream",则下面的方法则应改为:getMyStream
68      * @return InputStream  
69      */  
70     public InputStream getInputStream() {   
71          
72         // 文件所放的文件夹 , 有关路径问题,请参考另一篇博文:http://www.cnblogs.com/xiaoMzjm/p/3878758.html
73         String path = getServletContext().getRealPath("/")+"\DownLoadFile\";
74          
75         // 下载路径
76         String downLoadPath = path + fileName;
77          
78         // 输出
79         try {   
80             return new FileInputStream(downLoadPath);   
81         } catch (FileNotFoundException e) {   
82             e.printStackTrace();   
83         }   
84         return null;   
85     }   
86 }
原文地址:https://www.cnblogs.com/xiaoMzjm/p/3879048.html