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.
- Download and install Java JDK 7(Oracle one or the OpenJDK)
- Download and Install IDE like Eclipse or Netbeans(Preferred)
- Create a Dropbox Developer Account(Skip this step is you already have it)
- Create an App at https://www.dropbox.com/developers/apps and Select Full access
- Save your APP key and APP secret for later USE
- Download the Dropbox Java SDK from https://www.dropbox.com/developers/core/sdk
- 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
- Now you are ready to code :D
This was it guys. Comment what you feel and what problems you face... Hola have a Good Day!!!!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); } }
Created new window in existing browser session. this is showing in my output window..what it means?
ReplyDeleteCan you please elaborate?
DeleteNeed API to just list the file.. Suppose I just need to list all the files/folders in heirarchy mode in my dropbox account.
ReplyDeleteThanks!
Please provide code for the same.
Hii!!
DeleteYou can consider the Atocloud's Salesforce Integration services They have integrated salesforce with SAP, Magento, Hubspot, Mailchimp, PHP, Xero, etc.
Those classes arent in dropbox SDK libs, i don't know where are you getting these...
ReplyDeleteThis Zip consits all the java classes you need
Deletehttps://www.dropbox.com/static/developers/dropbox-java-sdk-1.6.zip
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.
ReplyDeleteGuys, if u have problem in importing com.dropbox.client2 then download jar from following url
ReplyDeletehttp://www.java2s.com/Code/Jar/d/Downloaddropboxjavaapi1311jar.htm
How to get uploaded file link using java code . so i will use that link in future
ReplyDelete