2015-10-26 44 views
1

我想從S3等外部源下載多個文件,創建一個包含所有這些文件的zip文件,並向用戶提供一個鏈接以下載該文件zip文件。使用Java將多個文件並行下載到S3中的zip文件

很顯然,我可以按順序處理文件,讀取每個文件的輸入流並將其寫入ZipOutputStream

如何並行讀取所有輸入文件流並寫入單個輸出流,以便我可以向用戶展示下載鏈接,而不必等到zip文件完全寫入?

我當前的代碼:

String realpath = getServletContext().getRealPath("/"); 
response.setContentType("application/zip"); 

response.setHeader("Content-Disposition","attachment; filename="+fi.replace('/', '-')+"_"+ff.replace('/', '-')+".zip"); 

ServletOutputStream out = null; 
ZipOutputStream zipfile = null; 

try 
{ 
    List<Object[]> cfdis = /*my hibernate criteria source, your Database?*/ 
    out = response.getOutputStream(); 
    zipfile = new ZipOutputStream(out); 
    ZipEntry zipentry = null; 
    for(Object[] cfdi:cfdis) 
    { 
     zipentry = new ZipEntry(cfdi[1].toString()+".xml"); 
     zipfile.putNextEntry(zipentry); 
     InputStream in = new FileInputStream(new File(realpath+cfdi[0].toString())); 
     byte[] bytes = new byte[FILEBUFFERSIZE]; 
     int bytesRead; 
     while ((bytesRead = in.read(bytes)) != -1) 
     { 
      zipfile.write(bytes, 0, bytesRead); 
     } 
    } 
+0

@Muhammadismail而不是將代碼放在評論中,請編輯您的問題,並在其中包含代碼。 – Cinnam

+0

希望我已經解決了這個問題。 – wOxxOm

+0

非常感謝@wOxxOm –

回答

1

的第一個文件&開始寫它讀取流的OutputStream

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    ServletOutputStream sos = response.getOutputStream(); 
    ZipOutputStream zos = new ZipOutputStream(sos); 

    try { 
     S3ServiceWrapper s3Service = new S3ServiceWrapper(); 
     ZipEntry zipentry = null; 
     byte bytes[] = new byte[4096]; 
     response.setContentType("application/zip"); 
     response.setHeader("Content-Disposition", "attachment; filename=java-s3-download.ZIP"); 

     for (String objectKey : objectKeys) { 
      String name = objectKey.substring(objectKey.lastIndexOf("/"), objectKey.length()); 
      log.info("Start Writing File::" + name); 

      zipentry = new ZipEntry(name); 
      zos.putNextEntry(zipentry); 
      InputStream in = s3Service.downloadFileAsStream(bucketName, objectKey); 
      int bytesRead = -1; 

      while ((bytesRead = in.read(bytes)) != -1) { 
       zos.write(bytes, 0, bytesRead); 
      } 

      log.info("Finsih Writing File::" + name); 
      in.close(); 
     } 
    } catch (Exception e) 

    { 
     e.printStackTrace(); 
    } finally { 
     zos.flush(); 
     zos.closeEntry(); 
     zos.close(); 
     sos.close(); 
    } 

} 

public InputStream downloadFileAsStream(String bucketName, String objectKey) { 
    if (s3Service == null) { 
     return null; 
    } 

    try { 
     GetObjectRequest s3ObjectReq = new GetObjectRequest(bucketName, objectKey); 
     log.info("Downloading file having key = " + objectKey); 
     long startTime=System.currentTimeMillis(); 
     S3Object downlodedObjectMD = s3Service.getObject(s3ObjectReq); 
     log.info("Time to load Stream is "+(System.currentTimeMillis()-startTime)+" ms"); 
     return downlodedObjectMD.getObjectContent(); 

    } catch (Exception e) { 
     log.error("EXCEPTION = " + e.getMessage(), e); 
    } 
    return null; 
} 
+0

你的代碼幫了我很多。 –

相關問題