java - 从 DropBox 下载文件到本地机器

标签 java upload dropbox

我想使用 java DropBox API 从我的保管箱帐户下载文件。我试过使用这段代码,但是当我想将文件下载到我的系统时,它显示了文件和文件夹的列表,这怎么可能

这是我的代码:-

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

最佳答案

这是一个使用 Dropbox 下载文件的基本示例。它不包括文件下载的进度,这只是为了直接下载文件。

此示例使用 DbxClientV2

使用 dropbox API v2
        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);
            }

希望这对您有所帮助并使用此示例并对其进行编辑以按照您希望的方式工作。

关于java - 从 DropBox 下载文件到本地机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22837030/

相关文章:

java - Java 中可通过强引用或弱引用访问的对象

java - 显示上传完成百分比

javascript - 如何使用 JavaScript 从 Dropbox 获取访问 token ?

c# - 获取本地保管箱同步状态

laravel - 如何使用flysystem从dropbox获取url?

java - 如何在 YouTube 播放器 Activity 中添加应用栏?

java - Helloworld 数据库 - android 与 sqlite 的连接

c# - 无法将大文件上传到 Google 文档

java - Eclipse + 将现有项目转变为 JP​​A 项目

jquery - Uploadify 拒绝上传 WMV、FLV 和 MP4 文件