struts2文件下载

来源:互联网 发布:svm 核函数 知乎 编辑:程序博客网 时间:2024/06/07 08:54

Struts2对文件的下载做了很优雅的处理,配置起来很简单,使用也很方便。

 

在本文中,你将学会最基础的download案例,和最优雅的download案例。

 

优雅之处:

 

1、不适用特定的new File加载文件。

2、文件名灵活,无需写死。

3、MimeType灵活,无需写死。

 

如主页的说明--“非技术流”, 表达不精确的地方,各位包涵。废话不说,上货!

 

环境: JDK6update16   EclipseJEE  3.4.2   Struts2.1.8


下载流程概览:

 

HttpRequest  --->   DownloadAction --->  SUCCESS Result -->   输出流

 

STEP01  写一个DownloadAction

Java代码 复制代码 收藏代码
  1. package study.action;   
  2.   
  3. import java.io.ByteArrayInputStream;   
  4. import java.io.InputStream;   
  5. import java.io.UnsupportedEncodingException;   
  6.   
  7. import javax.servlet.ServletContext;   
  8.   
  9. import org.apache.struts2.util.ServletContextAware;   
  10.   
  11. import com.opensymphony.xwork2.ActionSupport;   
  12.   
  13. public class DownloadAction extends ActionSupport implements  
  14.         ServletContextAware {   
  15.     private static final long serialVersionUID = 1L;   
  16.   
  17.     private ServletContext context;   
  18.   
  19.     private String filename;   
  20.   
  21.     private String mimeType;   
  22.   
  23.     private InputStream inStream;   
  24.   
  25.     @Override  
  26.     public String execute() throws Exception {   
  27.         mimeType = context.getMimeType(filename);   
  28.         return SUCCESS;   
  29.     }   
  30.   
  31.     public InputStream getInStream() {   
  32.         inStream = context.getResourceAsStream("/doc/" + filename);   
  33.         if (inStream == null) {   
  34.             inStream = new ByteArrayInputStream("Sorry,File not found !"  
  35.                     .getBytes());   
  36.         }   
  37.         return inStream;   
  38.     }   
  39.   
  40.     public String getMimeType() {   
  41.         return mimeType;   
  42.     }   
  43.   
  44.     public void setFilename(String filename) {   
  45.         try {   
  46.             this.filename = new String(filename.getBytes("ISO8859-1"),"GBK");   
  47.         } catch (UnsupportedEncodingException e) {   
  48.         }   
  49.     }   
  50.   
  51.     public String getFilename() {   
  52.         try {   
  53.             return new String(filename.getBytes(),"ISO8859-1");   
  54.         } catch (UnsupportedEncodingException e) {   
  55.             return this.filename;   
  56.         }   
  57.     }   
  58.   
  59.     @Override  
  60.     public void setServletContext(ServletContext context) {   
  61.         this.context = context;   
  62.     }   
  63.   
  64. }  

 

 说明:


1、在下载的Action中,必须有个InputStream类型的field和对应的get方法。


2、下载时方便,将文件名、MIMETYPE都写在了Action中。

 

 

然后,配合Result类型:

Java代码 复制代码 收藏代码
  1. <action name="download" class="study.action.DownloadAction">   
  2.     <result type="stream">   
  3.         <param name="contentType">${mimeType}</param>   
  4.         <param name="inputName">inStream</param>   
  5.         <param name="contentDisposition">attachment;filename="${filename}"</param>   
  6.     </result>   
  7. </action>  
 

最后,看图:


 


 

看看中文问题(具体的解决办法没有,这种只是在我的机子上可以。。。)


 


解释说明:

 

1、为了获取到MIMETYPE,利用了ServletContext的方法。所以必须获得ServlerContext这个对象。本例子中采用DI的方法,有Struts2在运行时注入。


2、为了能在HTTP Response中使用到 MIMETYPE,所以在Action中提供了对应的get方法,以供OGNL表达式需要。

3 、在jsp页面上展示下载的图片方式   <img src="mainPageAction!getImg.action?id=<s:property value="id"/>"/>

 

0 0