使用Java程序将文件上载到Google Drive的共享链接

使用Java程序将文件上载到Google Drive的共享链接,java,google-drive-api,Java,Google Drive Api,我希望将我的java应用程序中的文件上传到最终用户提供的共享google drive链接。最终用户已允许对共享的google drive文件夹具有“可编辑”权限。我在Google drive中没有看到任何API可以帮助我将文件上传到用户的共享Google drive链接。当从浏览器中访问这个共享链接时,它显示了通过共享链接映射到文件夹下的文件列表的网页,它允许我拖放文件在空白区域上载。因为这个链接是网页,所以我不能从java应用程序中使用,所以我需要类似的API Google Drive API

我希望将我的java应用程序中的文件上传到最终用户提供的共享google drive链接。最终用户已允许对共享的google drive文件夹具有“可编辑”权限。我在Google drive中没有看到任何API可以帮助我将文件上传到用户的共享Google drive链接。当从浏览器中访问这个共享链接时,它显示了通过共享链接映射到文件夹下的文件列表的网页,它允许我拖放文件在空白区域上载。因为这个链接是网页,所以我不能从java应用程序中使用,所以我需要类似的API

Google Drive API记录在

通常,您会将文档上载到Google Drive中您自己的根文件夹(“我的驱动器”),然后将文档移动或添加到其他用户共享的目标文件夹中。

因此,
supportsAllDrives=true
参数通知Google Drive您的应用程序旨在处理共享驱动器上的文件。但是还提到,
supportsAllDrives
参数的有效期至2020年6月1日。2020年6月1日之后,将假定所有应用程序都支持共享驱动器。因此,我尝试使用Google Drive V3 Java API,发现共享驱动器目前在V3 API的
Drive.Files.Create
类的
execute
方法中默认支持。附加示例代码段以供参考。此方法使用可恢复上载将文件上载到Google drive文件夹,并返回上载的文件ID

public static String uploadFile(Drive drive, String folderId , boolean useDirectUpload) throws IOException {

    /*
    * drive: an instance of com.google.api.services.drive.Drive class
    * folderId: The id of the folder where you want to upload the file, It can be
    * located in 'My Drive' section or 'Shared with me' shared drive with proper 
    * permissions.
    * useDirectUpload: Ensures whether using direct upload or Resume-able uploads.
    * */

    private static final String UPLOAD_FILE_PATH = "photos/big.JPG";
    private static final java.io.File UPLOAD_FILE = new java.io.File(UPLOAD_FILE_PATH);

    File fileMetadata = new File();
    fileMetadata.setName(UPLOAD_FILE.getName());
    fileMetadata.setParents(Collections.singletonList(folderId));
    FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);

    try {
        Drive.Files.Create create = drive.files().create(fileMetadata, mediaContent);
        MediaHttpUploader uploader = create.getMediaHttpUploader();
        //choose your chunk size and it will be automatically divided parts
        uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
        //As per Google, this enables gzip in future (optional) // got from another post
        uploader.setDisableGZipContent(false);
        //true enables direct upload, false resume-able upload 
        uploader.setDirectUploadEnabled(useDirectUpload);
        uploader.setProgressListener(new FileUploadProgressListener());
        File file =  create.execute();
        System.out.println("File ID: " + file.getId());
        return file.getId();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return null;
}