Struts2下载

 1 package com.tcf.action;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.InputStream;
 7 
 8 import com.opensymphony.xwork2.ActionSupport;
 9 
10 public class DownLoadAction extends ActionSupport {
11     private String inputPath;
12     private String fileName;
13     private InputStream  inputStream;
14     private String contentType;
15     
16     public String getInputPath() {
17         return inputPath;
18     }
19 
20 
21     public void setInputPath(String inputPath) {
22         this.inputPath = inputPath;
23     }
24 
25 
26     public String getFileName() {
27         return fileName;
28     }
29 
30 
31     public void setFileName(String fileName) {
32         this.fileName = fileName;
33     }
34     
35     public String getContentType() {
36         return contentType;
37     }
38 
39 
40     public InputStream getInputStream() {
41         return inputStream;
42     }
43 
44 
45     public void setInputStream(InputStream inputStream) {
46         this.inputStream = inputStream;
47     }
48 
49 
50     public void setContentType(String contentType) {
51         this.contentType = contentType;
52     }
53 
54     public String execute(){
55         try {
56             this.inputStream = new BufferedInputStream(new FileInputStream(inputPath+"/"+fileName));
57         } catch (FileNotFoundException e) {
58             // TODO Auto-generated catch block
59             e.printStackTrace();
60         }
61         return SUCCESS;
62     }
63 }

struts.xml

 1 <action name="download" class="com.tcf.action.DownLoadAction">
 2     <param name="inputPath">E:/temp</param>
 3     <param name="fileName">back.gif</param>
 4     <result type="stream">
 5         <param name="contentType">image/gif</param>
 6         <param name="inputName">inputStream</param>
 7         <!-- 附件 -->
 8         <param name="contentDisposition">
 9                 attachment;filename="${fileName}"
10         </param>
11         <param name="bufferSize">4096</param>
12     </result>
13 </action>

download.jsp

1 <a href="download.action" >下载</a>
原文地址:https://www.cnblogs.com/yunwei1237/p/5899667.html