从DropBox下载文件到本地计算机

5
我希望使用Java DropBox API下载我的Dropbox账户中的文件。我尝试使用以下代码,但是它显示文件和文件夹列表,而我想要将文件下载到我的系统中。请问如何实现?
以下是我的代码:
Scanner tokenScanner = new Scanner(tokensFile);       
       String ACCESS_TOKEN_KEY = tokenScanner.nextLine();    // Read key
       String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
       tokenScanner.close(); //Close Scanner
       //Re-auth
       AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET);
       mDBApi.getSession().setAccessTokenPair(reAuthTokens);
       Entry entries = mDBApi.metadata("/", 20, null, true, null);
       for (Entry e: entries.contents) {
        if(!e.isDeleted){
         if(e.isDir){
          System.out.println("Folder ---> " + e.fileName() );
         } else {
          //  this will download the root level files.
          System.out.println("File ---->" + e.fileName());
          DropboxInputStream inputStream = mDBApi.getFileStream(e.path,null);
          OutputStream out=new FileOutputStream(e.fileName());
          byte buf[]=new byte[1024];
          int len;
          while((len=inputStream.read(buf))>0)
           out.write(buf,0,len);
               out.close();
               inputStream.close();
               System.out.println("File is created....");

兄弟,你真的需要修复你的缩进... - Maciej Cygan
太多代码了!仅张贴相关部分。 - Philipp Sander
@Philipp:我已经删除了不必要的代码,请帮我看看。 - Saurabh Garg
1个回答

9
这是一个基本示例,用于使用Dropbox下载文件的IT技术相关内容。它不包括文件下载进度,仅适用于简单直接的文件下载。
此示例使用Dropbox API v2,并使用DbxClientV2
        try
            {
                //output file for download --> storage location on local system to download file
                OutputStream downloadFile = new FileOutputStream("C:\\.....");
                try
                {
                FileMetadata metadata = client.files().downloadBuilder("/root or foldername here/Koala.jpg")
                        .download(downloadFile);
                }
                finally
                {
                    downloadFile.close();
                }
            }
            //exception handled
            catch (DbxException e)
            {
                //error downloading file
                JOptionPane.showMessageDialog(null, "Unable to download file to local system\n Error: " + e);
            }
            catch (IOException e)
            {
                //error downloading file
                JOptionPane.showMessageDialog(null, "Unable to download file to local system\n Error: " + e);
            }

希望这可以帮到你,使用这个例子并编辑它以使其按照你想要的方式工作。

非常好的回答! - GOXR3PLUS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接