从DropBox下载文件到本地机器

时间:2014-04-03 11:58:53

标签: java upload dropbox

我想使用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....");

1 个答案:

答案 0 :(得分:7)

这是下载使用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);
            }

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