如何使用保管箱同时上传多个文件 java api

How to upload multiple files at the same time using the dropbox java api

我想知道如何使用 java 保管箱 api 将多个文件上传到保管箱。我现在想知道这一点,当我想上传一个文件夹时,我递归地遍历文件夹中的每个文件并一个一个地上传它们。但是,我发现这太慢了。所以,我想我可以一次上传一个文件夹中的所有文件。但是,我该怎么做呢?我应该创建 n 个线程并且每个线程上传一个文件还是什么?

是的,您可以使用多线程调用 API 并上传文件。您可以使用 Thread Pools 相同。您需要确定创建不会影响性能的线程数的要点。

下面的代码将允许您在 5 个单独的线程中上传 10 个文件(在 fileLocations 数组中提供)。

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