DownloadZip.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.zhcs.dt.service.information.annualPlan;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.util.List;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipOutputStream;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. public class DownloadZip {
  13. // public static void batchDownLoadFile(HttpServletRequest request,HttpServletResponse response,String filename,String[] filepath,String[] documentname,String loginname){
  14. // byte[] buffer = new byte[1024];
  15. // Date date=new Date();
  16. // //生成zip文件存放位置
  17. // String strZipPath = Constant.exportAddress +loginname+date.getTime()+".zip";
  18. // File file=new File(Constant.exportAddress);
  19. // if(!file.isDirectory() && !file.exists()){
  20. // //创建单层目录
  21. // // f.mkdir();
  22. // // 创建多层目录
  23. // file.mkdirs();
  24. // }
  25. // try {
  26. // ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
  27. // // 需要同时下载的多个文件
  28. // for (int i = 0; i < filepath.length; i++) {
  29. // File f=new File(filepath[i]);
  30. // FileInputStream fis = new FileInputStream(f);
  31. // System.out.println(documentname[i]);
  32. // out.putNextEntry(new ZipEntry(documentname[i]));
  33. // //设置压缩文件内的字符编码,不然会变成乱码
  34. // out.setEncoding("GBK");
  35. // int len;
  36. // // 读入需要下载的文件的内容,打包到zip文件
  37. // while ((len = fis.read(buffer)) > 0) {
  38. // out.write(buffer, 0, len);
  39. // }
  40. // out.closeEntry();
  41. // fis.close();
  42. // }
  43. // out.close();
  44. // PublicMethod.downLoadFile(request, response, strZipPath, filename+".zip");
  45. // File temp=new File(strZipPath);
  46. // if(temp.exists()){
  47. // temp.delete();
  48. // }
  49. // } catch (Exception e) {
  50. // System.out.println("文件下载错误");
  51. // }
  52. // }
  53. public static void downloadZip(HttpServletResponse response,HttpServletRequest request,List<String> paths,String path)throws Exception{
  54. OutputStream out = response.getOutputStream();
  55. File zip = new File("文档.zip");
  56. ZipFiles(paths, zip);
  57. FileInputStream inStream = new FileInputStream(zip);
  58. byte[] buf = new byte[4096];
  59. int readLength;
  60. while (((readLength = inStream.read(buf)) != -1)) {
  61. out.write(buf, 0, readLength);
  62. }
  63. inStream.close();
  64. out.flush();
  65. out.close();
  66. }
  67. public static void ZipFiles(List<String> paths, java.io.File zipfile) {
  68. byte[] buf = new byte[1024];
  69. try {
  70. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
  71. zipfile));
  72. for (int i = 0; i < paths.size(); i++) {
  73. if (paths.get(i) != null && !paths.get(i).equals("")) {
  74. FileInputStream in = new FileInputStream(paths.get(i));
  75. File file = new File(paths.get(i));
  76. out.putNextEntry(new ZipEntry(file.getName()));
  77. int len;
  78. while ((len = in.read(buf)) > 0) {
  79. out.write(buf, 0, len);
  80. }
  81. out.closeEntry();
  82. in.close();
  83. }
  84. }
  85. out.close();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. finally{
  90. deleteFiles(paths);
  91. }
  92. }
  93. public static void deleteFiles(List<String> paths) {
  94. try {
  95. for (String string : paths) {
  96. File file = new File(string);
  97. file.delete();
  98. }
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }