cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We are making some updates so the Community might be down for a few hours on Monday the 11th of November. Apologies for the inconvenience and thank you for your patience. You can find out more here.

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: 

Java API V2 Session Upload

Java API V2 Session Upload

RTS S.
Helpful | Level 6

Can you post a simple example showing how to use:

  

uploadSessionStart
uploadSessionAppendBuilder
uploadSessionFinishBuilder

Including intermediate steps ... I am stuck on how to get a SessionID after calling uploadSessionStart.

An example of this process should be available on ALL platforms! Android AP1V1, was easy, I stumbled with this on Swifty, .NET was easy. Java is not obvious.








 
9 Replies 9

Greg-DB
Dropbox Staff

I don't believe we have a sample for that, but I'll be sure to pass this along as feedback. 

If you elaborate on what you're stuck on we'll be happy to help though. Please share your code and whatever output/errors you're getting.

Kannan G.1
Dropbox Staff

This API is kind of a mess right now.  We need to sort some things out to make it clearer, but for now, this might work:

 

```

DbxClientV2 client = ...

 

uploader = client.files.uploadSessionStart();

String sessionId;

try {

    uploader.getBody().write(first_chunk_of_upload)

    sessionId = uploader.finish()

} finally {

    uploader.close()

}

 

uploader = client.files.uploadSessionAppend(sessionId, offset)

try {

    uploader.getBody().write(second_chunk_of_upload)

    uploader.finish()

} finally {

    uploader.close()

}

 

cursor = UploadSessionCursor(sessionId, offset);

FileMetadata md = client.files.uploadSessionFinishBuilder(cursor, ...).run(third_chunk_of_upload);

```

 

I don't have time to test the code right now.  Lemme know if you run into any issues and I'll take a closer look.

RTS S.
Helpful | Level 6

Thank you ... I can work from this ... it was not obvious how you to get the session ID.

RTS S.
Helpful | Level 6

A comment ... the interface is a little strange ...

 

I get an OutputStream that I must write to for uploading the initial chunk.

I must supply an InputStream that the API reads from for the middle and last chunks.

Kannan G.1
Dropbox Staff

Yeah, sorry about that.  We're definitely going to change the interface to make it more consistent.

pankaj kumar t.
New member | Level 1

Hi @Kannan G thanks for the code sample,

at last i have successfully wrote an example and works like charm 🙂

DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial",
"en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
try {
URL url = new URL(url_d);
// Upload Files to Dropbox
String fileName = url_d.substring(url_d.lastIndexOf('/') + 1,
url_d.length());
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.addRequestProperty("Range", "bytes=0-256");
hc.connect();
DbxFiles.UploadSessionStartUploader re = client.files.uploadSessionStart();
re.getBody().write(IOUtils.toByteArray(hc.getInputStream()));
UploadSessionStartResult sa = re.finish();
String ss = sa.sessionId;
System.out.println(ss);
hc.disconnect();
HttpURLConnection hc1 = (HttpURLConnection) url.openConnection();
hc1.addRequestProperty("Range", "bytes=257-512");
hc1.connect();
UploadSessionAppendBuilder re1 = client.files.uploadSessionAppendBuilder(ss, 257);
re1.run(hc1.getInputStream());
hc1.disconnect();
HttpURLConnection hc2 = (HttpURLConnection) url.openConnection();
hc2.addRequestProperty("Range", "bytes=513-");
hc2.connect();
UploadSessionCursor usc = new UploadSessionCursor(ss, 513);
FileMetadata nn = client.files.uploadSessionFinishBuilder(
usc,
new CommitInfo("/" + fileName, DbxFiles.WriteMode.add,
false, new Date(), false))
.run(hc2.getInputStream());
System.out.println(nn.toStringMultiline());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UploadException e) {
e.printStackTrace();
} catch (DbxException e) {
e.printStackTrace();
}

Christian
New member | Level 2

hi @pankaj kumar t. where is url_d, you can post the url that you use?

Greg-DB
Dropbox Staff

Christian, it looks like Pankaj's example reads the data to upload from an arbitrary URL, likely specific to their app. When developing your own implementation, you can retrieve the data to upload from wherever it is in your app, e.g., a local file.

pankaj kumar t.
New member | Level 1

hi @christian i had written a logic to upload files to dropbox 

feel free to ask any doubt.

here is the code which upload entire file.

 

String url_d = "<your download url here>";

long buffer = 33554432; // 32mb buffer
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial",
"en_US");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
try {
URL url = new URL(url_d);
String sessionId = ";
// Upload Files to Dropbox
String fileName = java.net.URLDecoder.decode(url_d.substring(url_d.lastIndexOf('/') + 1,url_d.length()), "UTF-8");;
HttpURLConnection getLength = (HttpURLConnection) url.openConnection();
getLength.connect();
long size = getLength.getContentLengthLong();
// Start session
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.addRequestProperty("Range", "bytes=0-"+buffer);
hc.connect();
DbxFiles.UploadSessionStartUploader result = client.files.uploadSessionStart();
result.getBody().write(IOUtils.toByteArray(hc.getInputStream()));
UploadSessionStartResult sa = result.finish();
sessionId = sa.sessionId;
hc.disconnect();
if (size > 0) {
//System.out.println("large ");

long s2 = buffer;
long tmp = 0;
//System.out.println("0"+" "+buffer);
while (tmp <= size && (buffer + s2) < size) {
buffer++;
tmp = buffer + s2;
if (tmp < size) {
System.out.println(buffer + "\t" + (tmp)+"\t"+(tmp-buffer));
// Append to session
HttpURLConnection hc1 = (HttpURLConnection) url.openConnection();
hc1.addRequestProperty("Range", "bytes="+buffer+"-"+tmp);
hc1.setReadTimeout(10000);
hc1.connect();
UploadSessionAppendBuilder re1 = client.files.uploadSessionAppendBuilder(sessionId, buffer);
re1.run(hc1.getInputStream());
hc1.disconnect();
buffer = tmp;
}
}
if((tmp+1)+buffer>size){
// finish session
HttpURLConnection hc2 = (HttpURLConnection) url.openConnection();
hc2.addRequestProperty("Range", "bytes="+(tmp+1)+"-"+size);
hc2.setReadTimeout(10000);
hc2.connect();
UploadSessionCursor usc = new UploadSessionCursor(sessionId, (tmp+1));
FileMetadata nn = client.files.uploadSessionFinishBuilder(
usc,
new CommitInfo("/" + fileName, DbxFiles.WriteMode.add,false, new Date(), false))
.run(hc2.getInputStream());
// End
System.out.println("Done");
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UploadException e) {
e.printStackTrace();
} catch (DbxException e) {
e.printStackTrace();
}

 

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    pankaj kumar t. New member | Level 1
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Christian New member | Level 2
  • User avatar
    Kannan G.1 Dropbox Staff
What do Dropbox user levels mean?