前言
Struts2的文件下载Action与普通的Action并没有什么太大的不同,仅仅是该Action需要提供一个返回InputStream流的方法,该输入流代表了被下载文件的入口,同时在配置文件中配置Action的result类型为stream。
文件下载
Action类的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
   | public class FileDownloadAction extends ActionSupport {
      private static final long serialVersionUID = 1L;
      // 文件的Mime类型     private String contentType; 	        // 要下载的文件名称     private String fileName;          private File[] files;
      public String list() throws Exception {         File f = new File(getDownloadFilePath());         // 获取目录下保存的文件         files = f.listFiles();         return "list";     }
     /*     * 定义一个返回InputStream的方法 该方法将被作为下载文件的入口, 且需要配置Stream类型结果时指定inputName参数     * inputName参数的值就是该方法去掉get前缀,首字母小写的字符串     */     public InputStream getDownloadFile() throws Exception {         String filePath = getDownloadFilePath() + "/" + fileName; 		         InputStream is = new FileInputStream(new File(filePath));         return is;     }
      //设置下载文件的名称     public String getDownloadFileName() {         String downFileName = fileName; 		         // get userAgent         String userAgent = ServletActionContext.getRequest().getHeader("User-Agent");
          try {             // Edge Browser or IE Browser             if (userAgent.contains("Edge") || userAgent.contains("Trident") ||  userAgent.contains("MSIE")) {                 downFileName = java.net.URLEncoder.encode(downFileName, "UTF-8").replace("+", "%20");             } else {                 downFileName = new String(downFileName.getBytes("UTF-8"), "ISO-8859-1");             }         } catch (UnsupportedEncodingException e) { 			 e.printStackTrace();         }             return downFileName;     } 	     public String getDownloadFilePath() throws Exception {         String filePath = getClass().getResource("/").toURI().getPath();         filePath = filePath.replace("WEB-INF/classes/", "download"); 		         return filePath;     } 	     public File[] getFiles() {         return files;     }
      public void setFiles(File[] files) {         this.files = files;     }
      public void setFileName(String fileName) throws UnsupportedEncodingException{         //下载文件名如果包含中文需要转码,避免出现乱码         fileName = URLDecoder.decode(fileName,"UTF-8");         this.fileName = fileName;     }
      public String getFileName() {		         return fileName;     } 	        public String getContentType() {         return contentType;     }
      public void setContentType(String contentType) {         this.contentType = contentType;     } }
  | 
 
Struts.xml的配置
配置下载文件的Action时,需要配置一个类型为stream的结果,需要指定如下4个属性。
- contentType: 指定被下载文件的文件类型
 
- inputName: 指定被下载文件的入口输入流
 
- contentDisposition: 指定下载的文件名
 
- bufferSize: 指定下载文件时的缓冲大小
 
1 2 3 4 5 6 7 8 9 10 11 12
   | <action name="download" class="com.learn.FileDownloadAction.FileDownloadAction">     <result type="stream"> 	   <!-- 指定下载文件的MIME类型 --> 	   <param name="contentType">${contentType}</param> 	   <!-- 下载的文件数据存放的方法,该方法返回一个InputStream 例如取值为inputStream的属性需要编写getInputStream()方法 --> 	   <param name="inputName">downloadFile</param> 	   <!--下载时,客户端显示的下载的文件名 --> 	   <param name="contentDisposition">attachment;filename=${downloadFileName}</param> 	   <!-- 数据的缓冲大小 --> 	   <param name="bufferSize">4096</param>     </result> </action>
   | 
 
文件下载取消时出现异常
在struts2中使用result里type=”stream”的结果类型时,当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常。
异常原因分析:
- 配置其中result标签下的各个参数
 
- 从服务器中获取输入流,并同时与客户端建立输出流(服务器与客户端链接通过Socket进行连接)
 
- 当点击“保存”或“打开”时,开始传输数据。如果点击“取消”,关闭所有的流。
这里要注意的是,但是实际发现Socket并没有断开!并且流也没有关闭!这一点非常重要!所以在JSP容器通过Response获取输出流之前,前面的流并没有关闭,所以会造成该异常的报出。 
异常解决办法:
下载插件struts2-sunspoter-stream-1.0.jar,并复制在/WEB-INF/lib下。
CSDN下载地址:struts2-sunspoter-stream-1.0.jar
 
在Struts.xml 中配置:
 
1 2 3
   | <result-types>     <result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX" /> </result-types>
   | 
 
3.在下载的Action中配置result的类型为streamx:
1 2 3 4 5 6 7 8 9 10
   | <action name="download" class="com.learn.FileDownloadAction.FileDownloadAction">     <result type="streamx">         <!-- 下载的文件数据存放的方法,该方法返回一个InputStream 例如取值为inputStream的属性需要编写getInputStream()方法 -->         <param name="inputName">downloadFile</param>         <!--下载时,客户端显示的下载的文件名 -->         <param name="contentDisposition">attachment;fileName=${downloadFileName}</param>         <!-- 数据的缓冲大小 -->         <param name="bufferSize">4096</param>     </result> </action>
   | 
 
PS(附言):Demo下载地址