dimid
dimid

Reputation: 7631

Upload a file to my dropbox from a java servlet web-application

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.

Upvotes: 0

Views: 5286

Answers (3)

dimid
dimid

Reputation: 7631

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;         
    }
}

Upvotes: 1

user94559
user94559

Reputation: 60143

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

Upvotes: 1

Related Questions