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.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值