简体   繁体   English

google drive api java更新文件的内容

[英]google drive api java update content of a file

I'm getting illegal argument exception while using update mediaContent. 使用更新mediaContent时出现非法参数异常。

private static File updateFile(Drive service, String fileId, 
         String newMimeType, String newFilename, boolean newRevision) {
      try {
        // First retrieve the file from the API.
        File file = service.files().get(fileId).execute();


        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;
      }
    }

I'm getting this exception: 我收到此异常:

 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.<init>(Drive.java:3163)
at com.google.api.services.drive.Drive$Files.update(Drive.java:3113)
at sai.sai.vasa.main(vasa.java:247)

when I am updating only metadata of a file the method is update(fileId,fileMetadata), it is working fine. 当我仅更新文件的元数据时,该方法为update(fileId,fileMetadata),它工作正常。 but when I tried to add mediacontent as third parameter in update as defined in api I am getting the above exception. 但是当我尝试在api中定义的update中将mediacontent添加为第三个参数时,出现了以上异常。

How to solve this error? 如何解决这个错误? How to do it in version3. 在版本3中如何做。 I want to do it in version3 only but I couldn't find the update code in drive API. 我只想在version3中执行此操作,但是在驱动器API中找不到更新代码。

From the Google Drive Rest API documentation sample , update is executed like below 从Google Drive Rest API文档示例 ,更新如下执行

File updatedFile = service.files().update(fileId, file, mediaContent).execute() ; File updatedFile = service.files().update(fileId, file, mediaContent).execute() ;

Where mediaContent is of FileContent class. 其中mediaContentFileContent类的。

I don't know what you are trying to accomplish with typecasting it to AbstractInputStreamContent but there's no need to typecast it as such due to FileContent class is already a subclass of AbstractInputStreamContent . 我不知道您要尝试将类型转换为AbstractInputStreamContent的目的是什么,但是由于FileContent类已经是AbstractInputStreamContent的子类,因此无需进行类型转换。

You can check that out over here 你可以在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM