struts2基于注解的文件下载

参数说明:

type = "stream",这里的type一定要等于stream;

对params的几个参数的解释:

"contentType", "application/octet-stream" : 文件格式;

"inputName", "attachstream" :获取文件的方法名;这里的attachstream需要和action里的attachstream对应,类型就是InputStream;

"contentDisposition", "attachment;filename="${attachname}"":文件内容(显示的)属性,

这里的filename="${attachname}" :下载之后的文件名;这里需要在action里定义一个变量,去获取下载文件的文件名,包括后缀,例如:下载的文件为**/**/upload/test.jpg,那么需要定义一个变量attachname,变量名一定要对应,然后,attachname = test.jsp;

"bufferSize", "4096" :限定下载文件 缓冲区的值;

通过上述属性的解释,我们可以知道,在action里需要定义的变量有以下几个:

private InputStream attachstream;//文件读取流对象

private String attachname;//下载文件的名字

然后需要对应的getter/setter方法,

action实现代码如下:

 1 @Action(value = "/admin/toDownload",
 2             results = {@Result(name = "download", type = "stream", 
 3                         params = {
 4                         "contentType", "application/octet-stream",
 5                         "inputName", "attachstream",
 6                         "contentDisposition", "attachment;filename="${attachname}"",
 7                         "bufferSize", "4096"
 8             })})
 9     public String toDownload(){
10         String path = ServletActionContext.getServletContext().getRealPath("/" + fileName);//fileName是页面传过来的参数
11         try {
12             attachstream = new FileInputStream(path);
13             String []attachnames = fileName.split("/");
14             attachname = attachnames[attachnames.length - 1];
15         } catch (FileNotFoundException e) {
16             e.printStackTrace();
17         }
18         return "download";
19     }
View Code

我自己写的:

 1 @ParentPackage("managerPackage")
 2 @Namespace("/districtTest")
 3 @Action("districtTest")
 4 @Results({
 5     @Result(name="districtTest_list",location="/manage/districtTest/districtTest_list.jsp"),
 6     @Result(name="districtTest_add",location="/manage/districtTest/districtTest_add.jsp"),
 7     @Result(name="districtTest_edit",location="/manage/districtTest/districtTest_edit.jsp"),
 8     @Result(name="districtTest_show",location="/manage/districtTest/districtTest_show.jsp"),
 9     @Result(name="districtTest_show0",location="/manage/districtTest/districtTest_show0.jsp"),
10     @Result(name = "download", type = "stream", 
11     params = {
12     "contentType", "application/octet-stream",
13     "inputName", "inputStream",
14     "contentDisposition", "attachment;filename="${attachname}"",
15     "bufferSize", "4096"
16 })
17     
18 })
19 
20 
21 
22 //下载
23     public String download(){
24         try {
25             
26             if(upload!=null&&!"".equals(upload)){
27                 String path = ServletActionContext.getServletContext().getRealPath("/" + upload);
28                 inputStream = new FileInputStream(path);
29                 String []attachnames = upload.split("/");
30                 attachname = attachnames[attachnames.length - 1];
31             }
32             return "download";
33         } catch (Exception e) {
34             // TODO: handle exception
35             Loggers.error("----DistrictTestAction的download方法错误"+e.getMessage());
36             e.printStackTrace();
37         }
38         return ERROR;
39     }
View Code
原文地址:https://www.cnblogs.com/yang1018/p/7169963.html