We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

RTS S.'s avatar
RTS S.
Helpful | Level 6
9 years ago

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 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.








 
  • 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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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.

  • RTS S.'s avatar
    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.'s avatar
    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.

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

  • pankaj kumar t.'s avatar
    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's avatar
    Christian
    New member | Level 2

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

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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.'s avatar
    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();
    }

     

     

About Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,875 PostsLatest Activity: 4 hours ago
323 Following

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 or Facebook.

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!