java 批量下載文件

經常遇到選擇多個文件進行批量下載的情況,可以先將選擇的所有的文件生成一個zip文件,然後再下載,該zip文件,即可實現批量下載,但是在打包過程中,常常也會出現下載過來的zip文件中裏面有亂碼的文件名,通過使用ant.jar中的org.apache.tools.zip裏的ZipOutPutStream爲實現編碼的設置。
代碼如下:
ant包引用
Xml代碼 複製代碼 收藏代碼
  1. <dependency>  
  2.   <groupId>ant</groupId>  
  3.   <artifactId>ant</artifactId>  
  4.   <version>1.6.5</version>  
  5. </dependency>   

壓縮下載的action代碼
Java代碼 複製代碼 收藏代碼
  1. 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, 08192)) != -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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章