分享

java web实现多文件的打包下载

 昵称21365845 2015-06-25

今天花了一天时间实现了一个多文件打包下载的功能,具体流程如下,创建文件夹->在文件夹下创建文件->向文件中写内容->将文件夹打成压缩包->删除文件夹->将压缩包发送给客户端->删除服务器端的压缩包。部分代码如下,以供以后参考:

  1. <p> // 创建文件夹  
  2.  private File createDir(String path) {  
  3.   File dirFile = null;  
  4.   try {  
  5.    dirFile = new File(path);  
  6.    if (!(dirFile.exists()) && !(dirFile.isDirectory())) {  
  7.     dirFile.mkdirs();  
  8.    }  
  9.   } catch (Exception e) {  
  10.    e.printStackTrace();  
  11.   }  
  12.   return dirFile;  
  13.  }</p><p> // 创建文件  
  14.  private File createFile(String path) {  
  15.   File file = new File(path);  
  16.   try {  
  17.    file.createNewFile();  
  18.   } catch (IOException e) {  
  19.    e.printStackTrace();  
  20.   }  
  21.   return file;  
  22.  }</p><p> // 删除文件  
  23.  public boolean delFile(String path) throws Exception {  
  24.   boolean result = false;  
  25.   File file=new File(path);    
  26.   if(file.exists()&&file.isFile())    
  27.   {  
  28.    file.delete();  
  29.    result = true;  
  30.   }  
  31.   return result;  
  32.  }</p><p> // 删除文件及文件夹  
  33.  private boolean delDir(File folder) {  
  34.   boolean result = false;  
  35.   try {  
  36.    String childs[] = folder.list();  
  37.    if (childs == null || childs.length <= 0) {  
  38.     if (folder.delete()) {  
  39.      result = true;  
  40.     }  
  41.    } else {  
  42.     for (int i = 0; i < childs.length; i++) {  
  43.      String childName = childs[i];  
  44.      String childPath = folder.getPath() + File.separator  
  45.        + childName;  
  46.      File filePath = new File(childPath);  
  47.      if (filePath.exists() && filePath.isFile()) {  
  48.       if (filePath.delete()) {  
  49.        result = true;  
  50.       } else {  
  51.        result = false;  
  52.        break;  
  53.       }  
  54.      } else if (filePath.exists() && filePath.isDirectory()) {  
  55.       if (delDir(filePath)) {  
  56.        result = true;  
  57.       } else {  
  58.        result = false;  
  59.        break;  
  60.       }  
  61.      }  
  62.     }  
  63.    }</p><p>   folder.delete();  
  64.   } catch (Exception e) {  
  65.    e.printStackTrace();  
  66.    result = false;  
  67.   }  
  68.   return result;  
  69.  }</p><p> /** 
  70.   *  
  71.   * @param zipFilePath 
  72.   *            打包文件存放路径 
  73.   * @param inputFolderName 
  74.   *            需要打包的文件夹 
  75.   * @throws Exception 
  76.   */  
  77.  public void zip(String zipFilePath, String inputFolderName)  
  78.    throws Exception {  
  79.   String zipFileName = zipFilePath; // 打包后文件名字  
  80.   File zipFile = new File(inputFolderName);  
  81.   zip(zipFileName, zipFile);  
  82.  }</p><p> private void zip(String zipFileName, File inputFolder) throws Exception {  
  83.   FileOutputStream fileOut = new FileOutputStream(zipFileName);  
  84.   ZipOutputStream out = new ZipOutputStream(fileOut);  
  85.   zip(out, inputFolder, "");  
  86.   out.close();  
  87.   fileOut.close();  
  88.  }</p><p> private void zip(ZipOutputStream out, File inputFolder, String base)  
  89.    throws Exception {  
  90.   if (inputFolder.isDirectory()) {  
  91.    File[] fl = inputFolder.listFiles();  
  92.    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));  
  93.    base = base.length() == 0 ? "" : base + "/";  
  94.    for (int i = 0; i < fl.length; i++) {  
  95.     zip(out, fl[i], base + fl[i].getName());  
  96.    }  
  97.   } else {  
  98.    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));  
  99.    FileInputStream in = new FileInputStream(inputFolder);  
  100.    int b;  
  101.    //System.out.println(base);  
  102.    while ((b = in.read()) != -1) {  
  103.     out.write(b);  
  104.    }  
  105.    in.close();  
  106.   }  
  107.  }</p>  


  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多