Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
RTS S.
10 years agoHelpful | Level 6
Java API V2 Session Upload
Can you post a simple example showing how to use:
uploadSessionStart uploadSessionAppendBuilder uploadSessionFinishBuilder Including intermediate steps ... I am stuck on how to get a Sess...
pankaj kumar t.
10 years agoNew 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();
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!