struts2下載文件功能(邊下載邊打包)

多個文件,目錄不同,通過條件查詢如何進行打包下載呢?

1.利用ZipEntry進i行文件的壓縮

2.前臺jsp傳入需要打包下載的一系列的文件的路徑(數組類型)。因爲是在checkBox中,表單提交會自動將其定義成數組。只需要將name名稱命名成後臺需要得到的路徑數組名稱

比如前臺

downLoadZip.jsp

--------checkBox處代碼-------------------------------

利用iterator迭代出來的filePath

<input type="checkbox" name="downLoadPaths"
value='<s:property value="filePath"/>'/>



後臺Action

private String[] downLoadPaths;

對downLoadPaths進行遍歷,打包。。。。

代碼:

Java代碼 
  1. /** 
  2.  * 批量下載(壓縮成zip,然後下載)。不創建臨時文件 
  3.  *  
  4.  * @author Cloudy 
  5.  *  
  6.  */  
  7. @Namespace("xxxxxx")  
  8. public class DownZipAction {  
  9.     /** 
  10.      *  
  11.      */  
  12.     private static final long serialVersionUID = 1L;  
  13.     // 傳遞一個List<String>()對象傳值路徑合集  
  14.     private String[] downLoadPaths;  
  15.     private OutputStream res;  
  16.     private ZipOutputStream zos;  
  17.     private String outPath;  
  18.   
  19.     // Action主方法  
  20.     @Action(value="DownLoadZip",results={@Result(name="nodata",location="/error.jsp"),  
  21.             @Result(name="success",location="xxxx.jsp")})  
  22.     public String downLoadZip() throws Exception {  
  23.         // 有數據可以下載  
  24.         if (downLoadPaths.length != 0) {  
  25.             // 進行預處理  
  26.             preProcess();  
  27.         } else {  
  28.             // 沒有文件可以下載,返回nodata  
  29.             return "nodata";  
  30.         }  
  31.         // 處理  
  32.         writeZip(downLoadPaths);  
  33.         // 後處理關閉流  
  34.         afterProcess();  
  35.         return SUCCESS;  
  36.     }  
  37.   
  38.     // 壓縮處理  
  39.     public void writeZip(String[] downLoadPaths) throws IOException {  
  40.         byte[] buf = new byte[8192];  
  41.         int len;  
  42.           
  43.         for (String filename : downLoadPaths) {  
  44.             File file = new File(filename);  
  45.             if (!file.isFile())  
  46.                 continue;  
  47.             ZipEntry ze = new ZipEntry(file.getName()); //apache jar的ZipEntry  
  48.             zos.putNextEntry(ze);  
  49.             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));  
  50.             while ((len = bis.read(buf)) > 0) {  
  51.                 zos.write(buf, 0, len);  
  52.             }  
  53.             bis.close();  
  54.             zos.closeEntry();  
  55.         }  
  56.     }  
  57.   
  58.     // 預處理  
  59.     public void preProcess() throws Exception {  
  60.         HttpServletResponse response = ServletActionContext.getResponse();  
  61.         res = response.getOutputStream();  
  62.         // 清空輸出流(在迅雷下載不會出現一長竄)  
  63.         response.reset();  
  64.         // 設定輸出文件頭  
  65.         response.setHeader("Content-Disposition""attachment;filename=document.zip");  
  66.         response.setContentType("application/zip");  
  67.         zos = new ZipOutputStream(res);  
  68.     }  
  69.   
  70.     // 後處理  
  71.     public void afterProcess() throws IOException {  
  72.   
  73.         zos.close();  
  74.         res.close();  
  75.     }  
  76.   
  77.     public OutputStream getRes() {  
  78.         return res;  
  79.     }  
  80.   
  81.     public void setRes(OutputStream res) {  
  82.         this.res = res;  
  83.     }  
  84.   
  85.     public ZipOutputStream getZos() {  
  86.         return zos;  
  87.     }  
  88.   
  89.     public void setZos(ZipOutputStream zos) {  
  90.         this.zos = zos;  
  91.     }  
  92.   
  93.     public String[] getDownLoadPaths() {  
  94.         return downLoadPaths;  
  95.     }  
  96.   
  97.     public void setDownLoadPaths(String[] downLoadPaths) {  
  98.         this.downLoadPaths = downLoadPaths;  
  99.     }  
  100.   
  101.     public String getOutPath() {  
  102.         return outPath;  
  103.     }  
  104.   
  105.     public void setOutPath(String outPath) {  
  106.         this.outPath = outPath;  
  107.     }  
  108.   
  109. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章