Source Code : To Upload and Download a File in Dropbox using Java

To Upload and Download a File in Dropbox using Java


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 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(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);  
 }   
  }