使用Java从Dropbox下载.txt文件

时间:2017-04-28 15:35:43

标签: java file download dropbox

嗨,我在最后一年,用Java(桌面应用程序)做项目。我正在编写一个java桌面应用程序类,将.txt文件下载到我的计算机桌面上。 我在Dropbox帐户的共享文件夹中下载文件时遇到问题。我已经完成了OAuth流程,并获得了令牌密钥和密码,但是当我使用Dropbox提供的代码时,我收到一条消息说“#34;导入com.dropbox无法解析"   Picture of The import com.dropbox cannot be resolved

我正在使用eclipse IDE,它在Dropbox上说我必须安装Java SDK,我已经下载了包,但不知道如何安装它。 Picture of the SDK folder downloaded

如果有人可以帮我解决导入问题或建议更简单的方法将文件从Dropbox下载到计算机,我将不胜感激。

感谢。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import com.dropbox.core.DbxAccountInfo;
import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;

public class Dropbox {

    private static final String DROP_BOX_APP_KEY = "<redacted>";
    private static final String DROP_BOX_APP_SECRET = "<redacted>";
    DbxClient dbxClient;

    public DbxClient authDropbox(String dropBoxAppKey, String dropBoxAppSecret)
            throws IOException, DbxException {
        DbxAppInfo dbxAppInfo = new DbxAppInfo(dropBoxAppKey, dropBoxAppSecret);
        DbxRequestConfig dbxRequestConfig = new DbxRequestConfig(
                "JavaDropboxTutorial/1.0", Locale.getDefault().toString());
        DbxWebAuthNoRedirect dbxWebAuthNoRedirect = new DbxWebAuthNoRedirect(
                dbxRequestConfig, dbxAppInfo);
        String authorizeUrl = dbxWebAuthNoRedirect.start();
        System.out.println("1. Authorize: Go to URL and click Allow : "
                + authorizeUrl);
        System.out
                .println("2. Auth Code: Copy authorization code and input here ");
        String dropboxAuthCode = new BufferedReader(new InputStreamReader(
                System.in)).readLine().trim();
        DbxAuthFinish authFinish = dbxWebAuthNoRedirect.finish(dropboxAuthCode);
        String authAccessToken = authFinish.accessToken;
        dbxClient = new DbxClient(dbxRequestConfig, authAccessToken);
        System.out.println("Dropbox Account Name: "
                + dbxClient.getAccountInfo().displayName);

        return dbxClient;
    }

    /* returns Dropbox size in GB */
    public long getDropboxSize() throws DbxException {
        long dropboxSize = 0;
        DbxAccountInfo dbxAccountInfo = dbxClient.getAccountInfo();
        // in GB :)
        dropboxSize = dbxAccountInfo.quota.total / 1024 / 1024 / 1024;
        return dropboxSize;
    }

    public void uploadToDropbox(String fileName) throws DbxException,
            IOException {
        File inputFile = new File(fileName);
        FileInputStream fis = new FileInputStream(inputFile);
        try {
            DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName,
                    DbxWriteMode.add(), inputFile.length(), fis);
            String sharedUrl = dbxClient.createShareableUrl("/" + fileName);
            System.out.println("Uploaded: " + uploadedFile.toString() + " URL "
                    + sharedUrl);
        } finally {
            fis.close();
        }
    }

    public void createFolder(String folderName) throws DbxException {
        dbxClient.createFolder("/" + folderName);
    }

    public void listDropboxFolders(String folderPath) throws DbxException {
        DbxEntry.WithChildren listing = dbxClient
                .getMetadataWithChildren(folderPath);
        System.out.println("Files List:");
        for (DbxEntry child : listing.children) {
            System.out.println("    " + child.name + ": " + child.toString());
        }
    }

    public void downloadFromDropbox(String fileName) throws DbxException,
            IOException {
        FileOutputStream outputStream = new FileOutputStream(fileName);
        try {
            DbxEntry.File downloadedFile = dbxClient.getFile("/" + fileName,
                    null, outputStream);
            System.out.println("Metadata: " + downloadedFile.toString());
        } finally {
            outputStream.close();
        }
    }

    public static void main(String[] args) throws IOException, DbxException {
        JavaDropbox javaDropbox = new JavaDropbox();
        javaDropbox.authDropbox(DROP_BOX_APP_KEY, DROP_BOX_APP_SECRET);
        System.out.println("Dropbox Size: " + javaDropbox.getDropboxSize()
                + " GB");
        javaDropbox.uploadToDropbox("happy.png");
        javaDropbox.createFolder("tutorial");
        javaDropbox.listDropboxFolders("/");
        javaDropbox.downloadFromDropbox("happy.png");

    }

}

0 个答案:

没有答案