java Google Drive API V3分段和可恢复上传

问题描述 投票:1回答:1

到目前为止,我在编写大文件的分段且可恢复的上载(> 5MB时需要帮助,到目前为止,我只能启动一次多载上载,但是无论用户暂停还是在网络故障期间,我都不知道如何恢复它。

通过“恢复”,我是说我不知道​​如何

1)获取已经上传到驱动器的总字节数>

2)如何在内容范围标题中使用该值

3)甚至如何通过用户交互来暂停此上传[executeAsInputStream()也许?]

这是我到目前为止所做的。即使我要强制停止应用程序并重新启动它,我也需要从停止上传的地方恢复代码]

   Drive service = GDrive.getService(); //Drive Specific Initialization Copied From QuickStart But with DriveScopes.FILES

   File fileMetadata = new File();
   fileMetadata.setName("Video.mp4"); //Video File
   fileMetadata.setMimeType("application/vnd.google-apps.video");

   java.io.File filePath = new java.io.File("E:\\large-file-60MB.mp4");//Large File Of 60 Mega Bytes
   FileContent mediaContent = new FileContent("video/mp4",filePath);

   Drive.Files.Create create=service.files().create(fileMetadata,mediaContent);

   MediaHttpUploader uploader=create.getMediaHttpUploader();
   uploader.setDirectUploadEnabled(false);                       //Use Resumable MultiPart Upload Protocol
   uploader.setChunkSize(2*MediaHttpUploader.MINIMUM_CHUNK_SIZE); //Chunks Of Bytes To Upload With Each Request

  // HttpHeaders headers=new HttpHeaders();
  // headers.put("Content-Range",?);          //This is not actual code which I used here but after reading the drive docs they talk about this header and I am not sure how or when to use it
  // uploader.setInitiationHeaders(headers);

   uploader.setProgressListener((uploading)->
   {
    switch (uploading.getUploadState())
    {
      case INITIATION_STARTED:System.out.println("Initiation has started!");
      break;
      case INITIATION_COMPLETE:System.out.println("Initiation is complete!");
      break;
      case MEDIA_IN_PROGRESS:
      System.out.println("Progress="+uploading.getProgress());
      System.out.println("Bytes="+uploading.getNumBytesUploaded());
      break;
      case MEDIA_COMPLETE:System.out.println("Upload is complete!");
    }
   });

   create.execute(); 
    

到目前为止,我在编写大文件(> 5MB)的分段且可恢复的上载时需要帮助,我只能启动一次多载上载,但是我不知道如何在用户暂停时恢复它……

java google-drive-api multipartform-data
1个回答
1
投票

虽然在一个答案中回答多个问题通常不适合Stackoverflow,但似乎所有这些都紧密相连,因此将概述可恢复上传,并尝试解决您的三点:

© www.soinside.com 2019 - 2024. All rights reserved.