如何更新google drive api v3 java中已有的文件

我尝试过以下代码……没有运气……

private void updateFile(Drive service, String fileId) { try { File file = new File(); /********/ final java.io.File fileToUpdate = new java.io.File("D:/Work Data/Files/pdf.pdf"); FileContent mediaContent = new FileContent("image/pdf", fileToUpdate); file = service.files().update(fileId, file, mediaContent).execute(); System.out.println(fileId); } catch (Exception e) { if (isDebug) { e.printStackTrace(); } } } 

也试过……

  private void updateFile(Drive service, String fileId) { try { File file = service.files().get(fileId).execute(); /********/ final java.io.File fileToUpdate = new java.io.File("D:/Work Data/Files/pdf.pdf"); FileContent mediaContent = new FileContent("image/pdf", fileToUpdate); file = service.files().update(fileId, file, mediaContent).execute(); System.out.println(fileId); } catch (Exception e) { if (isDebug) { e.printStackTrace(); } } } 

每次我执行代码我得到以下stacktrace:

 java.lang.IllegalArgumentException at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111) at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37) at com.google.api.client.googleapis.media.MediaHttpUploader.setInitiationRequestMethod(MediaHttpUploader.java:872) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.initializeMediaUpload(AbstractGoogleClientRequest.java:237) at com.google.api.services.drive.Drive$Files$Update.(Drive.java:3163) at com.google.api.services.drive.Drive$Files.update(Drive.java:3113) at com.test.DriveTester.updateFile(DriveTester.java:76) at com.test.DriveTester.main(DriveTester.java:64) 

谁能说出我做错了什么? 任何示例代码,即更新谷歌驱动器上已有文件的内容将有所帮助…

要更新文件内容,您可以使用Files:update ,此方法支持/上传URI并接受具有以下特征的上载媒体:

  • 最大文件大小:5120GB
  • 接受的媒体MIME类型:/

此方法通过两个单独的URI提供媒体上载function。 有关更多详细信息,请参阅有关媒体上载的文档。

  • 上传URI,用于媒体上传请求:

PUT https://www.googleapis.com/upload/drive/v2/files/fileId *元数据URI,仅用于元数据请求:

PUT https://www.googleapis.com/drive/v2/files/fileId

 private static File updateFile(Drive service, String fileId, String newTitle, String newDescription, String newMimeType, String newFilename, boolean newRevision) { try { // First retrieve the file from the API. File file = service.files().get(fileId).execute(); // File's new metadata. file.setTitle(newTitle); file.setDescription(newDescription); file.setMimeType(newMimeType); // File's new content. java.io.File fileContent = new java.io.File(newFilename); FileContent mediaContent = new FileContent(newMimeType, fileContent); // Send the request to the API. File updatedFile = service.files().update(fileId, file, mediaContent).execute(); return updatedFile; } catch (IOException e) { System.out.println("An error occurred: " + e); return null; } } 

您也可以查看此SO票 ,票据讨论上述错误。

我可以使用v3共享javascript代码以上传到现有文件

  const url = 'https://www.googleapis.com/upload/drive/v3/files/' + fileId + '?uploadType=media'; if(self.fetch){ var setHeaders = new Headers(); setHeaders.append('Authorization', 'Bearer ' + authToken.access_token); setHeaders.append('Content-Type', mime); var setOptions = { method: 'PATCH', headers: setHeaders, body: data }; fetch(url,setOptions) .then(response => { if(response.ok){ console.log("save to drive"); } else{ console.log("Response wast not ok"); } }) .catch(error => { console.log("There is an error " + error.message); });