如何使用dropbox java api同时上传多个文件 [英] How to upload multiple files at the same time using the dropbox java api

查看:190
本文介绍了如何使用dropbox java api同时上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用java dropbox api将多个文件上传到dropbox。我想知道这是当前的,当我想上传文件夹时,我递归浏览文件夹中的每个文件并逐个上传。但是,我发现这太慢了。所以,我认为我可以一次上传文件夹中的所有文件。但是,我该怎么做?我应该创建n个线程,每个线程上传一个文件或什么?

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?

推荐答案

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

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.

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

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

这篇关于如何使用dropbox java api同时上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆