简体   繁体   English

如何使用Java在Dropbox中上传多个文件

[英]How to upload multiple files in dropbox using java

I have created a java cucumber maven project. 我创建了一个Java Cucumber Maven项目。 Now I want to push all report in dropbox once execution of test script is done. 现在,我想在测试脚本执行完成后将所有报告推送到保管箱中。

My main goal is to push report folder on Dropbox. 我的主要目标是在Dropbox上推送报告文件夹。

I am using below maven dependency: 我正在使用以下Maven依赖项:

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

I know it's old dependency but code provided by Dropbox is only supported by this lib. 我知道它是旧的依赖项,但是Dropbox提供的代码仅受此库支持。 Later version is showing errors or deprecated methods. 更高版本显示错误或不推荐使用的方法。

Source: 资源:

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

Code I am using is as below: 我使用的代码如下:

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

The problem is when I am running this code. 问题是当我运行此代码时。 It stuck on below line: 它停留在下面的行中:

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

Any idea why? 知道为什么吗?

Environment 环境

  • Window 10 视窗10
  • Java 8 Java 8

If you have any workaround please share. 如果您有任何解决方法,请分享。

It will really help. 这真的有帮助。

The fact that it stuck on that line is normal. 它停留在那条线上的事实是正常的。 The program just expects the user's input from the console in order to proceed to the next line of code. 该程序只是希望从控制台输入用户的信息,以便继续进行下一行代码。

Greg has provided correct dependencies and example which leads me to accomplish by requirements. Greg提供了正确的依赖关系和示例,使我可以按要求完成。

The new version: 新版本:

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

There's an uploading example here: 这里有一个上传示例:

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

I also add an article below which helps me during the integration : 我还在下面添加了一篇文章,在集成过程中对我有帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM