0

I would like to use my webapp to create a file based on user input, and upload it to my dropbox. For some reason there is no tutorial for java on the Dropbox website. I've found two examples:
example1
example2.

The latter is from May 2013, however, both seem to use an old API, which is no longer supported. I'M looking for a working example using dropbox-core-sdk-1.7.jar.

3 Answers 3

4

I wrote a simplified uploader based on the examples

https://github.com/dimidd/dropbox-sdk-java/blob/master/examples/uploader/src/com/dropbox/core/examples/uploader/DbxUploader.java

Sign up to request clarification or add additional context in comments.

Comments

1

You might take a look at the examples folder in the Java SDK to see how the API differs from earlier versions. Also take a look at the documentation. For example, you'll probably want to use uploadFile: http://dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/com/dropbox/core/DbxClient.html#uploadFile(java.lang.String, com.dropbox.core.DbxWriteMode, long, java.io.InputStream).

(Sorry; I can't quite get that URL to parse properly.)

5 Comments

Thanks, in order to run them you'll need to download jackson-dattabind which isn't supplied with the SDK
@Dimid, which example? In any case, all the examples appear to include a pom.xml so you can install dependencies via Maven.
com.dropbox.core.examples.authorize
Looks like jackson-databind is properly listed in pom.xml, so it should get installed with the rest of the dependencies. Does it not?
It is, my comment is intended for those who don't use Maven
1

First create a DbxClient (shown in the examples), then upload:

/**
* Upload a file to your Dropbox
* @param srcFilename path to the source file to be uploaded 
*  e.g. /tmp/upload.txt
* @param destFilename path to the destination.
*  Must start with '/' and must NOT end with'/' 
*  e.g. /target_dir/upload.txt 
* @param dbxClient a DbxClient created using an auth-token for your Dropbox app
* @throws IOException 
*/
public void upload (String srcFilename, String destFilename, DbxClient dbxClient) throws IOException {

    File uploadFile = new File (srcFilename);
    FileInputStream uploadFIS;
    try {
        uploadFIS = new FileInputStream(uploadFile);
    }

    catch (FileNotFoundException e1) {          
        e1.printStackTrace();
        System.err.println("Error in upload(): problem opening " + srcFilename);
        return;
    }

    String targetPath = destFilename; 
    try {
        dbxClient.uploadFile(targetPath, DbxWriteMode.add(), uploadFile.length(), uploadFIS);
    } 

    catch (DbxException e) {
        e.printStackTrace();
        uploadFIS.close();
        System.err.println("Error in upload(): " + e.getMessage());
        System.exit(1);
        return;         
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.