Thursday 30 October 2014

Textual description of firstImageUrl

How to Upload Images to DropBox In Java

This Tutorial explains how to upload images to drop box and get the public url of uploaded image .
First of all we have to create a DropBox API app using app console. Once you create the app , you can get App key and secret key in the app properties .

Now add following dependency in your pom file .
<dependency>
 <groupId>com.dropbox.core</groupId>
 <artifactId>dropbox-core-sdk</artifactId>
 <version>1.7.7</version>
</dependency>
Now this java program will do the rest . Replace your app ket and secret key in program . Run this java program from command line and it will ask for the code , you will get the code by following the url printed on the console.

For getting the public url , we just need to use createShareableUrl of the dropboxClient class.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;


public class UploadImages
{
 public static void main(String[] args) throws IOException, DbxException
 {
  final String DROP_BOX_APP_KEY = "APPKEY";
        final String DROP_BOX_APP_SECRET = "SECRETKEY";
        
        String rootDir = "C:\\Users\\Downloads\\";

        DbxAppInfo dbxAppInfo = new DbxAppInfo(DROP_BOX_APP_KEY, DROP_BOX_APP_SECRET);

        DbxRequestConfig reqConfig = new DbxRequestConfig("javarootsDropbox/1.0",
            Locale.getDefault().toString());
        DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(reqConfig, dbxAppInfo);

        
        String authorizeUrl = webAuth.start();
        System.out.println("1. Go to this URL : " + authorizeUrl);
        System.out.println("2. Click \"Allow\" (you might have to log in first)");
        System.out.println("3. Copy the authorization code and paste here ");
        String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

        
        
        DbxAuthFinish authFinish = webAuth.finish(code);
        String accessToken = authFinish.accessToken;

        DbxClient client = new DbxClient(reqConfig, accessToken);

        System.out.println("account name is : " + client.getAccountInfo().displayName);

        
        
        File inputFile = new File(rootDir +"images\\"+ "javaroots.jpg");
        FileInputStream inputStream = new FileInputStream(inputFile);
        try {
         
            DbxEntry.File uploadedFile = client.uploadFile("/javaroots.jpg",
                DbxWriteMode.add(), inputFile.length(), inputStream);
            String sharedUrl = client.createShareableUrl("/javaroots.jpg");
            System.out.println("Uploaded: " + uploadedFile.toString() + " URL " + sharedUrl);
        } finally {
            inputStream.close();
        }
 }
}
Take reference from the this official drop box link.

You can download the full project from this link . Post Comments and Suggestions !!