2

I would like to know how to upload multiple files to dropbox using the java dropbox api. I would like to know this as currently, when I want to upload a folder, I recursively go through every file in the folder and upload them one by one. However, I find this too slow. So, I thought that I could just upload all the files in a folder at once. But, how would I do this? Should I create n number of threads and each thread uploads a single file or what?

4
  • Please have a look javapapers.com/java/dropbox-java-api-tutorial Commented Aug 14, 2015 at 12:17
  • @SubodhJoshi I have already checked that and found nothing about uploading multiple files at once. Commented Aug 14, 2015 at 14:11
  • The Dropbox API doesn't currently offer a way to upload multiple files with one API call, but we're tracking this as a feature request. Commented Aug 14, 2015 at 14:45
  • oh ok. but, will using multiple threads work? Commented Aug 14, 2015 at 17:24

1 Answer 1

5

Yes, you can call the API using multiple threads and upload files. You can use Thread Pools for the same. You need to identify the point for creating the number of threads that will not impact performance.

Below code will let you upload 10 files(provided in fileLocations array) in 5 separate threads.

public class UploadThread implements Runnable {

    private String fileLocation;

    public UploadThread(String s){
        this.fileLocation=s;
    }

    @Override
    public void run() {
       //your api call to upload file using fileLocation
    }

    @Override
    public String toString(){
        return this.command;
    }
}

public class UploadExecutor{

    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(5);

        String[] fileLocations = new String[10];

        for (int i = 0; i < 10; i++) {

            Runnable worker = new UploadThread(fileLocations[i]);

            executor.execute(worker);
        }
        executor.shutdown();

        while (!executor.isTerminated()) { }

        System.out.println("Finished uploading");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that's exactly what I needed!

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.