如何使用 java 在保管箱中上传多个文件

How to upload multiple files in dropbox using java

我创建了一个 java cucumber maven 项目。现在我想在测试脚本执行完成后将所有报告推送到保管箱中。

我的主要目标是在 Dropbox 上推送报告文件夹。

我正在使用以下 maven 依赖项:

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

我知道这是旧的依赖项,但 Dropbox 提供的代码仅受此库支持。更高版本显示错误或已弃用的方法。

来源:

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

我使用的代码如下:

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

问题是我在运行这段代码的时候。它卡在下面一行:

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

知道为什么吗?

环境

如果您有任何解决方法,请分享。

这真的很有帮助。

卡在那条线上是正常的。该程序只需要来自控制台的用户输入,以便继续执行下一行代码。

Greg 提供了正确的依赖项和示例,使我能够按要求完成。

新版本:

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

这里有一个上传示例:

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

我还在下面添加了一篇文章,它在集成过程中对我有帮助:

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