批量打包 下载文件

1. 把文件生成到tomcat目录下,这种写法对磁盘读写太大,而且时间比较慢
private void downloadAllLogs(HttpServletRequest request, HttpServletResponse response)
   {
      String fileName = request.getParameter("fileName");
      String rootPath = "";
      String fileSeparator = "";
      String logPath = rootPath + fileSeparator + "logs\\";
      
      ZipOutputStream zos = null;
      String newFileName = fileName + ".zip";
      try
      {
         zos = new ZipOutputStream(new FileOutputStream(newFileName));
         Zip(logPath, logPath.lastIndexOf("\\"), zos);
         
         zos.closeEntry();
         zos.close();       
      }
      catch (FileNotFoundException e)
      {
         throw new UnsupportedOperationException("FileNotFoundException Exception", e);
      }
      catch (IOException e)
      {
         throw new UnsupportedOperationException("IOException Exception", e);
      }
   }
<pre name="code" class="java">private void Zip(String path, int baseIndex, ZipOutputStream out) throws IOException
   {
      File file = new File(path);
      File[] files = null;
      if(file.isDirectory())
      {
         files = file.listFiles();
      }
      else
      {
         files = new File[1];
         files[0] = file;
      }
      
      for(File f : files)
      {
         if(f.isDirectory())
         {
            Zip(f.getPath(), baseIndex, out);
         }
         else
         {
            
            String pathName = f.getPath().substring(baseIndex + 1);
            ZipEntry ze = new ZipEntry(pathName);
            ze.setSize(f.length());
            ze.setTime(f.lastModified());
            out.putNextEntry(ze);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
            int c;
            while((c = bis.read()) != -1)
            {
               out.write(c);
            }            
            bis.close();
         }
      }
      
   }
 

2. 增加内存,不对磁盘进行读写,直接生成文件; 但是如果文件数量大,内容多的话,读取速度太慢

private void downloadAllLogs(HttpServletRequest request, HttpServletResponse response)
   {
      String fileName = request.getParameter("fileName");
      String rootPath = "";
      String fileSeparator = "";
      String logPath = rootPath + fileSeparator + "logs\\";
      
      ZipOutputStream zos = null;
      ByteArrayOutputStream baos = null;
      try
      {
         
         baos = new ByteArrayOutputStream();
         
         zos = new ZipOutputStream(baos);
         Zip(logPath, logPath.lastIndexOf("\\"), zos);
         
         zos.closeEntry();
         zos.flush();
         zos.close();
         
         byte[] tba = baos.toByteArray();
         baos.flush();
         baos.close();
         
         response.reset();
         response.setContentType(RequestHandler.DEFAULT_CONTENT_TYPE);
         response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
         
         long start = System.currentTimeMillis();
         ServletOutputStream servletOS = response.getOutputStream();
         servletOS.write(tba);
         
         servletOS.flush();
         servletOS.close();
      }
      catch (FileNotFoundException e)
      {
         throw new UnsupportedOperationException("FileNotFoundException Exception", e);
      }
      catch (IOException e)
      {
         throw new UnsupportedOperationException("IOException Exception", e);
      }
      
   }

   <pre name="code" class="java">private void Zip(String path, int baseIndex, ZipOutputStream out) throws IOException
   {
      long start = System.currentTimeMillis();
      System.out.println("循环压缩文件执行时间start:" + start);
      File file = new File(path);
      File[] files = null;
      if(file.isDirectory())
      {
         files = file.listFiles();
      }
      else
      {
         files = new File[1];
         files[0] = file;
      }
      
      for(File f : files)
      {
         String pathName = f.getPath().substring(baseIndex + 1);
         out.putNextEntry(new ZipEntry(pathName));
         
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
         
         int readLine = 0;
         int bufferSize = (int) f.length();
         byte[] buffer = new byte[bufferSize];
         while((readLine = bis.read(buffer, 0 , bufferSize)) != -1)
         {
            out.write(buffer, 0, readLine);
         }
         
         bis.close();
      }
      
      long end = System.currentTimeMillis() - start;
      System.out.println("循环压缩文件执行时间end:" + end);
      
   }

 

3. 优化读取速度


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值