struts2利用注解完成文件下载

来源:互联网 发布:手机虚化算法 编辑:程序博客网 时间:2024/05/18 01:16
  1. package com.figo.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.UnsupportedEncodingException;  
  8.   
  9. import org.apache.commons.io.FileUtils;  
  10. import org.apache.struts2.ServletActionContext;  
  11. import org.apache.struts2.convention.annotation.ParentPackage;  
  12. import org.apache.struts2.convention.annotation.Results;  
  13. import org.apache.struts2.convention.annotation.Result;  
  14.   
  15. import com.opensymphony.xwork2.ActionSupport;  
  16. import com.opensymphony.xwork2.ModelDriven;  
  17. import com.opensymphony.xwork2.Validateable;  
  18. import com.opensymphony.xwork2.ValidationAwareSupport;  
  19.   
  20. /** 
  21.  * 所下载文件相关的的几个属性:文件格式 contentType,  
  22.  * 获取文件名的方法inputName, 
  23.  * 文件内容(显示的)属性contentDisposition,  
  24.  * 限定下载文件 缓冲区的值bufferSize 
  25.  * */  
  26.   
  27. @Results({ @Result(name = "success", type = "stream", params = { "contentType",  
  28.         "application/octet-stream;charset=ISO8859-1""inputName",  
  29.         "inputStream""contentDisposition",  
  30.         "attachment;filename=\"Readme.txt\"""bufferSize""4096" }) })  
  31. public class DownloadAction extends ActionSupport {  
  32.   
  33.     private static final long serialVersionUID = 8784555891643520648L;  
  34.     private String STORAGEPATH = "/upload/Readme.txt";  
  35.   
  36.     private String fileName;// 初始的通过param指定的文件名属性  
  37.     private String storageId;  
  38.     private String inputPath;// 指定要被下载的文件路径  
  39.   
  40.     public String getFileName() {  
  41.         return fileName;  
  42.     }  
  43.   
  44.     public void setFileName(String fileName) {  
  45.         this.fileName = fileName;  
  46.     }  
  47.   
  48.     public void setInputPath(String inputPath) {  
  49.         this.inputPath = inputPath;  
  50.     }  
  51.   
  52.     public String getStorageId() {  
  53.         return storageId;  
  54.     }  
  55.   
  56.     public void setStorageId(String storageId) {  
  57.         this.storageId = storageId;  
  58.     }  
  59.   
  60.     // 如果下载文件名为中文,进行字符编码转换  
  61.     public String getDownloadFileName() {  
  62.         String downloadFileName = fileName;  
  63.   
  64.         try {  
  65.             downloadFileName = new String(downloadFileName.getBytes(),  
  66.                     "ISO8859-1");  
  67.         } catch (UnsupportedEncodingException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.   
  71.         return downloadFileName;  
  72.     }  
  73.   
  74.     public InputStream getInputStream() throws Exception {  
  75.         /** 
  76.          * 下载用的Action应该返回一个InputStream实例,  
  77.          * 该方法对应在result里的inputName属性值为targetFile 
  78.          **/  
  79.         return ServletActionContext.getServletContext().getResourceAsStream(  
  80.                 STORAGEPATH);  
  81.     }  
  82.   
  83.     public String execute() throws Exception {  
  84.         return SUCCESS;  
  85.     }  
  86.   
  87. }  

  view plaincopy

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags"%>  
  4.   
  5. <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >  
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head>  
  8. <title>Download</title>  
  9. </head>  
  10. <body>  
  11.     <s:a href="download.action">Download</s:a>  
  12. </body>  
  13. </html>  

代码其实很简单,最关键的就是getInputStream()方法,返回一个InputStream实例,还有就是@Results的配置。

        测试页面:download.jsp(注意:这段代码使用了struts2的约定方式,在JSP页面中的action名称为download.action,利用约定自动找到名为DownloadAction的类进行处理)。另外在web.xml中要将拦截器的匹配方式改为/*。

图中包的名称为,此时action的名称前不需要配命名空间,但如果写成第二行的包名称,那么action名称应该为test/download

另外,文中需要下载的资源放在项目文件下,即/upload/Readme.txt",所以获得下载路径可以用

  1. ServletActionContext.getServletContext().getResourceAsStream(  
  2.                 STORAGEPATH);  

但是,如果下载文件的路径path="E:\\Readme.txt"这样的,那么上段代码就改成

return new FileInputStream(path);  


0 0
原创粉丝点击