• Java批量文件打包下载


    经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名,通过使用ant.jar中的org.apache.tools.zip里的ZipOutPutStream为实现编码的设置。 
    代码如下: 

    ant包引用 

    [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代码 

    [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/

  • 相关阅读:
    cf854B Maxim Buys an Apartment
    Snuke's Coloring 2-1
    P1087 FBI树
    Card Game for Three
    Many Formulas
    排队
    苹果消消乐(尺取法)
    猴子选大王(约瑟夫)
    进制转化
    UIProgress控件的属性和方法
  • 原文地址:https://www.cnblogs.com/abc8023/p/5913425.html
Copyright © 2020-2023  润新知