使用Java程序将文件上传到Google云端硬盘的共享链接 [英] File Upload to Shared Link of the Google Drive using Java program

查看:106
本文介绍了使用Java程序将文件上传到Google云端硬盘的共享链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将文件从我的Java应用程序上传到最终用户提供的共享google驱动器链接.最终用户已允许对共享的google驱动器文件夹进行可以编辑"权限.我没有在Google驱动器中看到任何API可以帮助我将文件上传到用户的共享Google驱动器链接.当从浏览器访问此共享链接时,它将显示一个网页,该页面显示了通过该共享链接映射的文件夹下的文件列表,它允许我将拖放文件拖放到空白区域以进行上传.由于此链接是网页,因此无法从Java应用程序中使用,因此正在寻找类似的API.

I wish to upload files from my java application to the shared google drive link provided by the end user. End user has allowed 'can edit' permission to the google drive folder that is shared. I do not see any API in Google drive that helps me in uploading files to the user's shared google drive link. When this shared link is accessed from the browser, it shows the web page that shows list of files under folder mapped thru the shared link and it allows me to drag drop file on the blank area to upload. As this link is the web page, I can't use from java application hence looking for similar API.

推荐答案

按照此

As per this documentation, the supportsAllDrives=true parameter informs Google Drive that your application is designed to handle files on shared drives. But it is also mentioned that the supportsAllDrives parameter will be valid until June 1, 2020. After June 1, 2020, all applications will be assumed to support shared drives. So I tried with the Google Drive V3 Java API, and found that shared drives are currently supported by default in the execute method of Drive.Files.Create class of V3 APIs. Attaching a sample code snippet for your reference. This method uploadFile uploads a file to Google drive folder using resume-able upload and returns the uploaded fileId.

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

这篇关于使用Java程序将文件上传到Google云端硬盘的共享链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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