Search code examples
javadropboxdropbox-api

How to upload multiple files in dropbox using java


I have created a java cucumber maven project. Now I want to push all report in dropbox once execution of test script is done.

My main goal is to push report folder on Dropbox.

I am using below maven dependency:

<dependency>
    <groupId>com.dropbox.core</groupId>
    <artifactId>dropbox-core-sdk</artifactId>
    <version>1.7.2</version>
</dependency>

I know it's old dependency but code provided by Dropbox is only supported by this lib. Later version is showing errors or deprecated methods.

Source:

https://www.dropbox.com/developers-v1/core/start/java

Code I am using is as below:

public class DropBoxUpload {
    public static void main(String[] args) throws IOException, DbxException {
        // Get your app key and secret from the Dropbox developers website.
        final String APP_KEY = "MyAppKey";
        final String APP_SECRET = "MyAppSecretKey";

        DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);

        DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0",
            Locale.getDefault().toString());
        DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);

        // Have the user sign in and authorize your app.
        String authorizeUrl = webAuth.start();
        System.out.println("1. Go to: " + authorizeUrl);
        System.out.println("2. Click \"Allow\" (you might have to log in first)");
        System.out.println("3. Copy the authorization code.");

        String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

        // This will fail if the user enters an invalid authorization code.
        DbxAuthFinish authFinish = webAuth.finish(code);
        String accessToken = authFinish.accessToken;

        DbxClient client = new DbxClient(config, accessToken);

        System.out.println("Linked account: " + client.getAccountInfo().displayName);

        File inputFile = new File("working-draft.txt");
        FileInputStream inputStream = new FileInputStream(inputFile);
        try {
            DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
                DbxWriteMode.add(), inputFile.length(), inputStream);
            System.out.println("Uploaded: " + uploadedFile.toString());
        } finally {
            inputStream.close();
        }

        DbxEntry.WithChildren listing = client.getMetadataWithChildren("D:\\MavenJenkinsCI\\target\\cucumber-html-reports");
        System.out.println("Files in the root path:");
        for (DbxEntry child : listing.children) {
            System.out.println("    " + child.name + ": " + child.toString());
        }

        FileOutputStream outputStream = new FileOutputStream("magnum-opus.txt");
        try {
            DbxEntry.File downloadedFile = client.getFile("/magnum-opus.txt", null,
                outputStream);
            System.out.println("Metadata: " + downloadedFile.toString());
        } finally {
            outputStream.close();
        }
    }

The problem is when I am running this code. It stuck on below line:

   String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

Any idea why?

Environment

  • Window 10
  • Java 8

If you have any workaround please share.

It will really help.


Solution

  • Greg has provided correct dependencies and example which leads me to accomplish by requirements.

    The new version:

    https://github.com/dropbox/dropbox-sdk-java

    There's an uploading example here:

    https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/upload-file/src/main/java/com/dropbox/core/examples/upload_file/Main.java

    I also add an article below which helps me during the integration :

    http://blog.camilolopes.com.br/tag/dropbox-token/