cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted

Cancelling a file upload or download using Java 2 API

Explorer | Level 4

I have implemented a file upload and download capability in my Android app using the example approaches posted on GitHub. However, I need a way to interrupt the process to support the notion of 'cancel' by the user. There doesn't seem to be a way to do this? Once I get this far:

FileMetadata metadata = dbxClient.files().uploadBuilder(uploadPath + "/" + localFile.getName()) 
.withMode(WriteMode.OVERWRITE)
.withClientModified(new Date(localFile.lastModified()))
.uploadAndFinish(in, progressListener);

This blocks internally to the API until the upload is complete. I realize I can use the 'limit' setting in a loop like this:

while ((size - uploaded) > CHUNKED_UPLOAD_CHUNK_SIZE)  { 
    dbxClient.files()
   .uploadSessionAppendV2(cursor)
   .uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE, progressListener); 
    uploaded += CHUNKED_UPLOAD_CHUNK_SIZE; 
    cursor = new UploadSessionCursor(sessionId, uploaded); 
}

And then check for an interrupted condition prior to initializing 'cursor' again, is this the only way? It means a delay to the user depending on how big "CHUNKED_UPLOAD_CHUNK_SIZE" is, and I'd rather not have to play games optimizing this value on the relatively low chance that the user cancels. I'd rather be able to interrupt the process whenever it needs to be interrupted.

6 Replies 6
Highlighted

Re: Cancelling a file upload or download using Java 2 API

Dropboxer
Dropboxer

When using the Dropbox API v2 Java SDK, you can cancel an in-progress upload by using the DbxUploader.abort method. That's available on its sub-classes for various use cases, such as UploadUploaderUploadSessionStartUploaderUploadSessionAppendV2Uploader, and UploadSessionFinishUploader

For example, you can get the UploadUploader from upload or UploadBuilder.start (as opposed to directly chaining on uploadAndFinish).

That would look something like this: first, set up and save your uploader:

UploadUploader uploader = dbxClient.files().uploadBuilder(path)
                        .withMode(WriteMode.OVERWRITE)
                        .withClientModified(new Date(localFile.lastModified()))
                        .start();

Then, start the upload (maybe on another thread):

fileMetadata = uploader.uploadAndFinish(in, progressListener);

And if/when you need to cancel that upload:

if (someCondition) {
    uploader.abort();
}

The DbxDownloader class doesn't offer an abort method, but you can use DbxDownloader.getInputStream to get an InputStream so you can more directly control if/when/how much data to read from it. 

Hope this helps! 

 

 

Highlighted

Re: Cancelling a file upload or download using Java 2 API

Explorer | Level 4

Hi - that helps a lot on the upload area.

For the download, I'm not sure I follow how getting a reference to the InputStream helps here. I suppose if I could read from it in "chunks" similar to upload, I could check every "chunk" to see if a cancel had been indicated, but I'm not sure how to do that.

Do you have a small code example to share?

Thanks!

Highlighted

Re: Cancelling a file upload or download using Java 2 API

Explorer | Level 4

Is something like this appropriate?

                DbxDownloader<FileMetadata> downloader = dbxClient.files().downloadBuilder(dropboxPath + "/" + localFile)
                        .start();
                InputStream inputStream = downloader.getInputStream();
                int count;
                long bytesWritten = 0;
                boolean keepGoin = true;
                byte data[] = new byte[WRITE_BUFFER];
                while ( keepGoin && ((count = inputStream.read(data, 0, WRITE_BUFFER)) != -1)) {
                    outputStream.write(data, 0, count);
                    bytesWritten += count;
                    progressListener.onProgress(bytesWritten);
                    if ( listener != null ) {
                        keepGoin = listener.cancelFileOperation();
                    }
                }
                if ( bytesWritten != size) {
                    // Means we were interrupted
                    throw new UserCancelException(InTouch.getInstance().getString(R.string.interrupted_exception_cancel_xfer));
                }
Highlighted

Re: Cancelling a file upload or download using Java 2 API

Dropboxer
Dropboxer

Apologies I didn't have a code sample for that handy, but yes, what you just shared looks like what I had in mind.

Highlighted

Re: Cancelling a file upload or download using Java 2 API

Explorer | Level 4

Greg - 

Once I issue .start(), do I need to do anything other than close the streams to correctly end this transaction?

Highlighted

Re: Cancelling a file upload or download using Java 2 API

Dropboxer
Dropboxer

You should just use DbxDownloader.close when you're finished with the downloader (which will close the InputStream returned by getInputStream for you), in addition to closing any streams you opened yourself.

Polls
Do you know how to organize your files and folders?
We have created a guide on folder best practice, so you can get organized now check it out here.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?