Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Google Drive - Resumable Upload

See more Google Drive Examples

Demonstrates how to do a resumable upload to Google Drive.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Url;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        success = true;

        // This example uses a previously obtained access token having permission for the
        // Google Drive scope.
        // See Get Google Drive OAuth2 Access Token

        Http http = new Http();

        http.setAuthToken("GOOGLE_DRIVE_ACCESS_TOKEN");

        // First we want to initiate the resumable upload.
        // We send a POST to get a session URI, which will be used to upload the file, and resume if needed.

        // Our POST will look like this:

        // POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable HTTP/1.1
        // Authorization: Bearer [YOUR_AUTH_TOKEN]          <-- Chilkat automatically adds this header because the AuthToken property was set.
        // Content-Length: 38                               <-- Chilkat will automatically add this header.
        // Content-Type: application/json; charset=UTF-8    <-- This is the type of this POST, and is specified in the call to HttpJson.
        // X-Upload-Content-Type: application/zip           <-- The type of file we'll be uploading
        // X-Upload-Content-Length: 1366807                 <-- The size of the file we'll be uploading
        //
        // {
        //   "name": "myObject"
        // }

        http.setRequestHeader("X-Upload-Content-Type", "application/zip");
        http.setRequestHeader("X-Upload-Content-Length", "1366807");

        JsonObject json = new JsonObject();
        json.updateString("name", "myZip");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int statusCode = resp.getStatusCode();
        if (statusCode != 200) {
            System.out.println(resp.getBodyStr());
            System.out.println("response status = " + statusCode);
            return;
        }

        // The session URI is in the "Location" response header:
        StringBuilder sbSessionUri = new StringBuilder();
        sbSessionUri.append(resp.getHeaderField("Location"));
        String sessionUri = sbSessionUri.getAsString();
        System.out.println("Session URI: " + sessionUri);

        // Here's a sample session URI:  https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=AEnB2UrszR8lDqlo3mtXJw_0rYI_Hi1oVh1NPFcLfoyBq9NcSjk85kFopoZaVEgT38OSmLl0XsObPf4iCSJwHlrOvvIJQ_ckIQ

        // -----------------------------------------------------------------------------
        // The next step is to upload the file using the session URI:

        // We'll send a PUT that looks like this:

        // PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
        // Content-Length: 1366807
        // Content-Type: application/zip
        //
        // [BYTES 0-1366806]

        // First remove the X-Upload-Content-Type and X-Upload-Content-Length request headers we specified earlier, so they don't get sent with subsequent requests.
        http.clearHeaders();

        // Because the file is likely large, we'll use HttpSReq which allows to to pass an HTTP request object where the body can be streamed directly from a file.
        HttpRequest req = new HttpRequest();

        req.setContentType("application/zip");
        req.setHttpVerb("PUT");

        Url url = new Url();
        url.parseUrl(sessionUri);

        req.setPath(url.getPathWithQueryParams());
        System.out.println("Path with Query Param: " + url.getPathWithQueryParams());

        // Specify that the request body is to be streamed directly from a file.
        try {
            req.streamBodyFromFile("qa_data/zips/big.zip");

            http.httpSReq(url.getHost(), url.getPort(), url.getSsl(), req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(resp.getBodyStr());
        System.out.println("response status = " + resp.getStatusCode());

        // Sample output:

        // {
        //  "kind": "drive#file",
        //  "id": "1rx20i53eurtkVQ-RT7Ry8Ct85PgPYMET",
        //  "name": "myZip",
        //  "mimeType": "application/zip"
        // }
        //
        // response status = 200
    }
}