问题
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?
回答1:
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");
}
}
来源:https://stackoverflow.com/questions/32009719/how-to-upload-multiple-files-at-the-same-time-using-the-dropbox-java-api