Jean-Marc Astesana
Jean-Marc Astesana

Reputation: 1292

Dropbox ChunkedUpload

I'm trying to implement chunked upload to Dropbox. I've written a code based on the example from the Dropbox API documentation, but it fails.

The following code throws that exception:

com.dropbox.client2.exception.DropboxIOException: Apache HTTPClient encountered an error. No response, try again. at com.dropbox.client2.RESTUtility.execute(Unknown Source) at com.dropbox.client2.DropboxAPI$ChunkedUploadRequest.upload(Unknown Source) at com.dropbox.client2.DropboxAPI$ChunkedUploader.upload(Unknown Source) at com.dropbox.client2.DropboxAPI$ChunkedUploader.upload(Unknown Source)

Here is the code:

/** Uploads content to Dropbox.
 * @param dropbox An already linked dropbox API
 * @param stream The stream to upload
 * @param length The number of bytes to upload
 * @param path The path where to store the uploaded content
 * @return a boolean, false if the upload was cancelled, true if it completes 
 * @throws IOException
 * @throws DropboxException
 */
public static boolean upload(DropboxAPI<?> dropbox, InputStream stream,
        long length, String path) throws IOException, DropboxException {
    // Create a chunked uploader
    ChunkedUploader uploader = dropbox.getChunkedUploader(stream, length);
    try {
        int tryCounter = 1;
        while (!uploader.isComplete()) {
            try {
                uploader.upload(); // The exception occurs on this line
            } catch (DropboxException e) {
                tryCounter++;
                if (tryCounter > 5) throw e; // Give up after a while.
                System.err.println ("Upload failed, trying again ...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
            }
        }
    } catch (DropboxPartialFileException e) {
        // Upload was cancelled
        return false;
    }
    // If the upload completes, determine the current revision of the path.
    String parentRev = null;
    try {
        Entry metadata = dropbox.metadata(path, 1, null, false, null);
        parentRev = metadata.rev;
    } catch (DropboxServerException e) {
        // If the error is not a FileNotFound error => It's really an error
        if (e.error!=DropboxServerException._404_NOT_FOUND) throw e;
    }
    // Finish the upload
    uploader.finish(path, parentRev);
    return true;
}

Any idea of what I'm doing wrong?

PS : Of course, I verified that other api functions are ok with the DropboxAPI instance I pass to that method.

Upvotes: 1

Views: 1145

Answers (1)

Jean-Marc Astesana
Jean-Marc Astesana

Reputation: 1292

I have submitted the problem to the Dropbox team. They just answer me it was a bug in the Dropbox API. Version 1.5.3 fixes it.

Upvotes: 1

Related Questions