免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3454 | 回复: 0
打印 上一主题 下一主题

JAVA ZIP压缩打包下载 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-22 10:40 |只看该作者 |倒序浏览
JAVA ZIP压缩打包下载






引用
java自带的类库,实现zip压缩打包文件下载(下面是我的完整下载servlet下载类代码,改下自己的包名即可使用)


Java代码
  1. 1.package com.kedacom.kdkk.controller.querymanager;   
  2. 2.  
  3. 3.import java.io.File;   
  4. 4.import java.io.FileInputStream;   
  5. 5.import java.io.IOException;   
  6. 6.import java.util.Date;   
  7. 7.  
  8. 8.import javax.servlet.ServletException;   
  9. 9.import javax.servlet.http.HttpServlet;   
  10. 10.import javax.servlet.http.HttpServletRequest;   
  11. 11.import javax.servlet.http.HttpServletResponse;   
  12. 12.import java.util.zip.*;   
  13. 13.public class BatchDownload extends HttpServlet {   
  14. 14.  
  15. 15. public BatchDownload() {   
  16. 16.  super();   
  17. 17. }   
  18. 18.  
  19. 19. public void doGet(HttpServletRequest request, HttpServletResponse response)   
  20. 20.   throws ServletException, IOException {   
  21. 21.  response.setContentType("APPLICATION/OCTET-STREAM");   
  22. 22.  response.setHeader("Content-Disposition","attachment; filename="+this.getZipFilename());   
  23. 23.  System.out.println("in BatchDownload................");   
  24. 24.  ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());      
  25. 25.     File[] files = new File[2];   
  26. 26.     files[0]=new File("D:/OptiCM-POC/KDKK3Project/KDKK/images/1.jpg");   
  27. 27.     files[1]=new File("D:/OptiCM-POC/KDKK3Project/KDKK/images/2.jpg");   
  28. 28.     zipFile(files, "", zos);      
  29. 29.     zos.flush();      
  30. 30.     zos.close();      
  31. 31.  
  32. 32. }   
  33. 33. private void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException {        
  34. 34.      for (int i=0;i<subs.length;i++) {   
  35. 35.       File f=subs[i];   
  36. 36.       zos.putNextEntry(new ZipEntry(baseName + f.getName()));      
  37. 37.       FileInputStream fis = new FileInputStream(f);      
  38. 38.       byte[] buffer = new byte[1024];      
  39. 39.       int r = 0;      
  40. 40.       while ((r = fis.read(buffer)) != -1) {      
  41. 41.           zos.write(buffer, 0, r);      
  42. 42.       }      
  43. 43.       fis.close();   
  44. 44.      }   
  45. 45. }   
  46. 46. private String getZipFilename(){   
  47. 47.  Date date=new Date();   
  48. 48.  String s=date.getTime()+".zip";   
  49. 49.  return s;   
  50. 50. }   
  51. 51.  
  52. 52. public void doPost(HttpServletRequest request, HttpServletResponse response)   
  53. 53.   throws ServletException, IOException {   
  54. 54.  this.doGet(request, response);   
  55. 55. }   
  56. 56.}   
  57. package com.kedacom.kdkk.controller.querymanager;

  58. import java.io.File;
  59. import java.io.FileInputStream;
  60. import java.io.IOException;
  61. import java.util.Date;

  62. import javax.servlet.ServletException;
  63. import javax.servlet.http.HttpServlet;
  64. import javax.servlet.http.HttpServletRequest;
  65. import javax.servlet.http.HttpServletResponse;
  66. import java.util.zip.*;
  67. public class BatchDownload extends HttpServlet {

  68. public BatchDownload() {
  69.   super();
  70. }

  71. public void doGet(HttpServletRequest request, HttpServletResponse response)
  72.    throws ServletException, IOException {
  73.   response.setContentType("APPLICATION/OCTET-STREAM");
  74.   response.setHeader("Content-Disposition","attachment; filename="+this.getZipFilename());
  75.   System.out.println("in BatchDownload................");
  76.   ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());   
  77.      File[] files = new File[2];
  78.      files[0]=new File("D:/OptiCM-POC/KDKK3Project/KDKK/images/1.jpg");
  79.      files[1]=new File("D:/OptiCM-POC/KDKK3Project/KDKK/images/2.jpg");
  80.      zipFile(files, "", zos);   
  81.      zos.flush();   
  82.      zos.close();   

  83. }
  84. private void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException {     
  85.       for (int i=0;i<subs.length;i++) {
  86.        File f=subs[i];
  87.        zos.putNextEntry(new ZipEntry(baseName + f.getName()));   
  88.        FileInputStream fis = new FileInputStream(f);   
  89.        byte[] buffer = new byte[1024];   
  90.        int r = 0;   
  91.        while ((r = fis.read(buffer)) != -1) {   
  92.            zos.write(buffer, 0, r);   
  93.        }   
  94.        fis.close();
  95.       }
  96. }
  97. private String getZipFilename(){
  98.   Date date=new Date();
  99.   String s=date.getTime()+".zip";
  100.   return s;
  101. }

  102. public void doPost(HttpServletRequest request, HttpServletResponse response)
  103.    throws ServletException, IOException {
  104.   this.doGet(request, response);
  105. }
  106. }
复制代码
引用
下面是web.xml里面对下载zip servlet的配置


Java代码
  1. 1.<servlet>   
  2. 2.    <servlet-name>imgZipDownload</servlet-name>   
  3. 3.    <servlet-class>com.kedacom.kdkk.controller.querymanager.BatchDownload</servlet-class>   
  4. 4.  </servlet>   
  5. 5.  <servlet-mapping>     
  6. 6.    <servlet-name>imgZipDownload</servlet-name>   
  7. 7.    <url-pattern>/zipDownload.do</url-pattern>   
  8. 8.  </servlet-mapping>  
  9. <servlet>
  10.     <servlet-name>imgZipDownload</servlet-name>
  11.     <servlet-class>com.kedacom.kdkk.controller.querymanager.BatchDownload</servlet-class>
  12.   </servlet>
  13.   <servlet-mapping>  
  14.     <servlet-name>imgZipDownload</servlet-name>
  15.     <url-pattern>/zipDownload.do</url-pattern>
  16.   </servlet-mapping>
复制代码
引用
下面是页面调用servlet


Java代码
  1. 1.<form action="zipDownload.do" method="post">   
  2. 2.            <input type="submit" value="下载"/>   
  3. 3.        </form>  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP