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

标签 java google-drive-api

我试过下面的代码......没有任何运气......

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

每次我执行代码时,我都会得到以下堆栈跟踪:

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 com.test.DriveTester.updateFile(DriveTester.java:76)
    at com.test.DriveTester.main(DriveTester.java:64)

谁能告诉我做错了什么?任何示例代码,即更新谷歌驱动器上现有文件的内容都会有所帮助......

最佳答案

对于 API v3,Android Enthusiast 提出的解决方案不幸地不起作用。 问题在于这一点:

 // First retrieve the file from the API.
File file = service.files().get(fileId).execute();

这样做会创建一个文件对象,并设置其 ID 字段,当执行更新时,它会抛出异常,因为 ID 元字段不可直接编辑。

您可以做的只是创建一个新文件:

File file = new File();

如示例所示,根据需要更改您想要的元数据并更新文件内容。

然后按照上面的建议简单地更新文件:

// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();

因此,基于 Android Enthusiast 解决方案的完整示例如下所示:

private static File updateFile(Drive service, String fileId, String newTitle,
String newDescription, String newMimeType, String newFilename, boolean newRevision) {
try {
// First create a new File.
File file = new File();

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

关于java - 如何更新 google drive api v3 java 中已有的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38539158/

相关文章:

java - 如何将同名的输入值提取到 servlet 中?

javascript - 更新存储在 Google Drive 上的文件的内容

google-drive-api - 是否有 google drive SDK 邮件列表或 RSS 提要?

javascript - 当 HTTP POST 请求因网络丢失而中断时会发生什么?

java - 从数据库获取联系人时出错

java - gcj 在 jar 文件中找不到主类

Google Drive API V3 如何正确转义查询参数?

java - 未找到 API 包 'memcache' 或调用 'Get()'

java - 如何使用 Netbeans IDE 8.0 为 Web 应用程序创建 jar 文件

java - 从 Java/Windows 创建新邮件(默认客户端)