Java批量文件打包下載

常常遇到選擇多個文件進行批量下載的狀況,能夠先將選擇的全部的文件生成一個zip文件,而後再下載,該zip文件,便可實現批量下載,可是在打包過程當中,經常也會出現下載過來的zip文件中裏面有亂碼的文件名,經過使用ant.jar中的org.apache.tools.zip裏的ZipOutPutStream爲實現編碼的設置。 
代碼以下: html

ant包引用 java

 

[html]  view plain  copy
 
 print?
  1. <span style="font-size:14px">Xml代碼    
  2. <dependency>    
  3.   <groupId>ant</groupId>    
  4.   <artifactId>ant</artifactId>    
  5.   <version>1.6.5</version>    
  6. </dependency>  </span>  


壓縮下載的action代碼 apache

 

 

[html]  view plain  copy
 
 print?
  1. <span style="font-size:14px">package demo.action;    
  2.     
  3. import java.io.BufferedInputStream;    
  4. import java.io.BufferedOutputStream;    
  5. import java.io.File;    
  6. import java.io.FileInputStream;    
  7. import java.io.FileOutputStream;    
  8. import java.io.IOException;    
  9. import java.io.InputStream;    
  10. import java.io.OutputStream;    
  11. import java.net.URLEncoder;    
  12.     
  13. import javax.servlet.http.HttpServletResponse;    
  14.     
  15. import org.apache.log4j.Logger;    
  16. import org.apache.struts2.ServletActionContext;    
  17. import org.apache.tools.zip.ZipEntry;    
  18. import org.apache.tools.zip.ZipOutputStream;    
  19.     
  20. import com.opensymphony.xwork2.ActionSupport;    
  21.     
  22. /**   
  23.  * 批量下載文件:   
  24.  *   使用ant.jar包中的org.apache.tools.zip.*完成壓縮,   
  25.  * java原生也有java.util.zip.*可是測試了下沒法搞定壓縮   
  26.  * 文件內文件名的中文問題     
  27.  * @author yangcong   
  28.  *    
  29.  */    
  30. public class BatchDownloadAction extends ActionSupport {    
  31.     
  32.     private Logger Log = Logger.getLogger(BatchDownloadAction.class);    
  33.     private static final String FilePath = "D:\\";    
  34.     
  35.     private static final long serialVersionUID = -8694640030455344419L;    
  36.     
  37.     public String execute() {    
  38.         //生成的ZIP文件名爲Demo.zip    
  39.         String tmpFileName = "Demo.zip";    
  40.         byte[] buffer = new byte[1024];    
  41.         String strZipPath = FilePath + tmpFileName;    
  42.         try {    
  43.             ZipOutputStream out = new ZipOutputStream(new FileOutputStream(    
  44.                     strZipPath));    
  45.             // 須要同時下載的兩個文件result.txt ,source.txt    
  46.             File[] file1 = { new File(FilePath+"test1.txt"),    
  47.                     new File(FilePath+"測試2.docx") };    
  48.             for (int i = 0; i file1.length; i++) {    
  49.                 FileInputStream fis = new FileInputStream(file1[i]);    
  50.                 out.putNextEntry(new ZipEntry(file1[i].getName()));    
  51.                 //設置壓縮文件內的字符編碼,否則會變成亂碼    
  52.                 out.setEncoding("GBK");    
  53.                 int len;    
  54.                 // 讀入須要下載的文件的內容,打包到zip文件    
  55.                 while ((len = fis.read(buffer)) > 0) {    
  56.                     out.write(buffer, 0, len);    
  57.                 }    
  58.                 out.closeEntry();    
  59.                 fis.close();    
  60.             }    
  61.             out.close();    
  62.             this.downFile(getResponse(), tmpFileName);    
  63.         } catch (Exception e) {    
  64.             Log.error("文件下載出錯", e);    
  65.         }    
  66.         return null;    
  67.     }    
  68.     
  69.     /**   
  70.      * 獲取Response   
  71.      * @return   
  72.      */    
  73.     private HttpServletResponse getResponse() {    
  74.         return ServletActionContext.getResponse();    
  75.     }    
  76.     
  77.     /**   
  78.      * 文件下載   
  79.      * @param response   
  80.      * @param str   
  81.      */    
  82.     private void downFile(HttpServletResponse response, String str) {    
  83.         try {    
  84.             String path = FilePath + str;    
  85.             File file = new File(path);    
  86.             if (file.exists()) {    
  87.                 InputStream ins = new FileInputStream(path);    
  88.                 BufferedInputStream bins = new BufferedInputStream(ins);// 放到緩衝流裏面    
  89.                 OutputStream outs = response.getOutputStream();// 獲取文件輸出IO流    
  90.                 BufferedOutputStream bouts = new BufferedOutputStream(outs);    
  91.                 response.setContentType("application/x-download");// 設置response內容的類型    
  92.                 response.setHeader(    
  93.                         "Content-disposition",    
  94.                         "attachment;filename="    
  95.                                 + URLEncoder.encode(str, "UTF-8"));// 設置頭部信息    
  96.                 int bytesRead = 0;    
  97.                 byte[] buffer = new byte[8192];    
  98.                 // 開始向網絡傳輸文件流    
  99.                 while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {    
  100.                     bouts.write(buffer, 0, bytesRead);    
  101.                 }    
  102.                 bouts.flush();// 這裏必定要調用flush()方法    
  103.                 ins.close();    
  104.                 bins.close();    
  105.                 outs.close();    
  106.                 bouts.close();    
  107.             } else {    
  108.                 response.sendRedirect("../error.jsp");    
  109.             }    
  110.         } catch (IOException e) {    
  111.             Log.error("文件下載出錯", e);    
  112.         }    
  113.     }    
  114. }    
  115.   
  116.   通過在windows環境下測試經過,使用struts2</span>  
[html]  view plain  copy
 
 print?
[html]  view plain  copy
 
 print?
  1. <span style="font-size:14px">   資源ant.jar下載地址:<target="_blank" href="http://blog.csdn.net/clare504/article/details/11962263">http://blog.csdn.net/clare504/article/details/11962263</a></span>  

http://blog.csdn.net/clare504/article/details/11962263/windows

相關文章
相關標籤/搜索