To Upload and Download a File in Dropbox using Java

Dropbox  JAVA APP


Hi fellas, I will now show you how to upload and Download Files in Dropbox using the default dropbox api. Here are the Steps to do that.

  1. Download and install  Java JDK 7(Oracle one or the OpenJDK)
  2. Download and Install IDE like Eclipse or Netbeans(Preferred)
  3. Create a Dropbox Developer Account(Skip this step is you already have it)
  4. Create an App at https://www.dropbox.com/developers/apps and Select Full access
  5. Save your APP key and APP secret for later USE
  6. Download the Dropbox Java SDK from https://www.dropbox.com/developers/core/sdk
  7. Fire your IDE and Import all the Jar files inside your "lib" folder of your dropbox SDK . If you dont know how to import ,then check some youtube videos to do this
  8. Now you are ready to code :D    

Here's The Code to upload and Download file.

 package javaapplication11;  
 import com.dropbox.client2.DropboxAPI;  
 import com.dropbox.client2.DropboxAPI.DropboxFileInfo;  
 import com.dropbox.client2.DropboxAPI.Entry;  
 import com.dropbox.client2.exception.DropboxException;  
 import com.dropbox.client2.session.AccessTokenPair;  
 import com.dropbox.client2.session.AppKeyPair;  
 import com.dropbox.client2.session.RequestTokenPair;  
 import com.dropbox.client2.session.Session.AccessType;  
 import com.dropbox.client2.session.WebAuthSession;  
 import com.dropbox.client2.session.WebAuthSession.WebAuthInfo;  
 import java.awt.Desktop;  
 import java.io.ByteArrayInputStream;  
 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.net.URL;  
 import javax.swing.JOptionPane;  
 /**  
  *  
  * @author pranay  
  */  
 public class JavaApplication11   
 {  
   private static final String APP_KEY = "Your APP key that you got from step 5";  
   private static final String APP_SECRET = "Your APP Secret that you got from step 5";  
   private static final AccessType ACCESS_TYPE = AccessType.APP_FOLDER;  
   private static DropboxAPI<WebAuthSession> mDBApi;  
   public static void main(String[] args) throws Exception {  
     AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);  
     WebAuthSession session = new WebAuthSession(appKeys, ACCESS_TYPE);  
     WebAuthInfo authInfo = session.getAuthInfo();  
     RequestTokenPair pair = authInfo.requestTokenPair;  
     String url = authInfo.url;  
     Desktop.getDesktop().browse(new URL(url).toURI());  

     JOptionPane.showMessageDialog(null, "Press ok to continue once you have authenticated.");  
     session.retrieveWebAccessToken(pair);  
     AccessTokenPair tokens = session.getAccessTokenPair();  
     System.out.println("Use this token pair in future so you don't have to re-authenticate each time:");  
     System.out.println("Key token: " + tokens.key);  
     System.out.println("Secret token: " + tokens.secret);  
     mDBApi = new DropboxAPI<WebAuthSession>(session);  
     System.out.println();  
     System.out.print("Uploading file...");  
     String fileContents = "Hello World!";  
     ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes());  
     Entry newEntry = mDBApi.putFile("/testing.txt", inputStream, fileContents.length(), null, null);  
     System.out.println("Done. \nRevision of file: " + newEntry.rev);  
     FileOutputStream outputStream = null;  
   File file = new File("//home/pranay/file.txt");  
   outputStream = new FileOutputStream(file);  
   DropboxFileInfo info = mDBApi.getFile("/testing.txt", null, outputStream, null);  
 }   
   }  
This was it guys. Comment what you feel and what problems  you face... Hola have a Good Day!!!! 

Comments

  1. Created new window in existing browser session. this is showing in my output window..what it means?

    ReplyDelete
  2. Need API to just list the file.. Suppose I just need to list all the files/folders in heirarchy mode in my dropbox account.

    Thanks!

    Please provide code for the same.

    ReplyDelete
    Replies
    1. Hii!!

      You can consider the Atocloud's Salesforce Integration services They have integrated salesforce with SAP, Magento, Hubspot, Mailchimp, PHP, Xero, etc.

      Delete
  3. Those classes arent in dropbox SDK libs, i don't know where are you getting these...

    ReplyDelete
    Replies
    1. This Zip consits all the java classes you need

      https://www.dropbox.com/static/developers/dropbox-java-sdk-1.6.zip

      Delete
  4. Here's the problem. When I put the jar on my build path, I'm able to import classes from com.dropbox.core but you're using classes from com.dropbox.client2. I don't have an option to import those.

    ReplyDelete
  5. Guys, if u have problem in importing com.dropbox.client2 then download jar from following url
    http://www.java2s.com/Code/Jar/d/Downloaddropboxjavaapi1311jar.htm

    ReplyDelete
  6. How to get uploaded file link using java code . so i will use that link in future

    ReplyDelete

Post a Comment

Popular posts from this blog

How to download Salesforce Change sets components?[Solved]

How to Open Process Builder's Process in Flow Designer?[Solved]