Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download file from DropBox to local Machine

I want to download a file from my dropbox account using java DropBox API. I have tried using this code but this is displaying the list of files and folder while i want to download files to my system how it is possible

Here's my code:-

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....");
like image 468
Saurabh Garg Avatar asked Apr 03 '14 11:04

Saurabh Garg


People also ask

How do I move files from Dropbox to my local computer?

Move Dropbox files and foldersClick Files. Hover over the file or folder you'd like to move, and click the “…” (ellipsis). Click Move. Click the folder you'd like to move it to or create a new folder.

How do I download from Dropbox to USB?

To put your files on your usb, just right-click on the files you'd like to transfer on your desktop, choose the “Copy” option from the menu that appears and then “Paste” on the preferred location (F:\, G:\, etc. ). Hope this is helpful to you!


1 Answers

This is a basic example for downloading a file that work with dropbox. it does not include an progress of file download, this is just for straight forward file download.

This example uses dropbox API v2 using 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);
            }

Hope this helps and use this example and edit it to work the way you want it to work.

like image 73
John Avatar answered Nov 15 '22 01:11

John