如何在谷歌驱动器api v3 java中更新已经存在的文件 [英] How to update already existing file in google drive api v3 java

查看:144
本文介绍了如何在谷歌驱动器api v3 java中更新已经存在的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



I have tried with the following code... Without any luck...

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

每次我执行代码我得到了以下堆栈跟踪:

With every time i execute the code i get the following 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.<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)

任何人都可以告诉我做错了什么吗?任何示例代码,即即时更新已经存在的文件在谷歌驱动器的内容将有所帮助...

Can anyone tell what i am doing wrong ? Any sample code for this i.e. updating the content of an already existing file on google drive will be helpful...

推荐答案

我可以使用v3共享上传到已有文件的JavaScript代码

I can share javascript code for uploading to an already existing file using v3

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

这篇关于如何在谷歌驱动器api v3 java中更新已经存在的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆