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;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DropboxService {
private static final Logger LOG = Logger.getLogger(DropboxService.class.getName());
private static final String APP_KEY = "APP_KEY";
private static final String APP_SECRET = "APP_SECRET";
private DbxClient client;
public DropboxService() throws IOException, DbxException {
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0", Locale.getDefault().toString());
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
// Have the user sign in and authorize your app.
String authorizeUrl = webAuth.start();
System.out.println("1. Go to: " + authorizeUrl);
System.out.println("2. Click Allow (you might have to log in first)");
System.out.println("3. Copy the authorization code.");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
// This will fail if the user enters an invalid authorization code.
DbxAuthFinish authFinish = webAuth.finish(code);
client = new DbxClient(config, authFinish.accessToken);
//System.out.println("Linked account: " + client.getAccountInfo().displayName);
}
public void uploadFile(String filename) throws IOException, DbxException {
File inputFile = new File(filename);
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt", DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
LOG.log(Level.INFO, "Uploaded: {0}", uploadedFile.toString());
} finally {
inputStream.close();
LOG.log(Level.INFO, "Upload finished");
}
}
private void listFiles() throws DbxException {
DbxEntry.WithChildren listing = client.getMetadataWithChildren("/");
System.out.println("Files in the root path:");
for (DbxEntry child : listing.children) {
System.out.println(" " + child.name + ": " + child.toString());
LOG.log(Level.INFO, " {0}: {1}", new Object[]{child.name, child.toString()});
}
}
private void downloadFile() throws FileNotFoundException, DbxException, IOException {
FileOutputStream outputStream = new FileOutputStream("/Users/cem/Desktop/magnum-opus.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/magnum-opus.txt", null, outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
LOG.log(Level.INFO, "Downloaded: {0}", downloadedFile.toString());
} finally {
outputStream.close();
LOG.log(Level.INFO, "Download finished");
}
}
public static void main(String[] args) throws IOException, DbxException {
DropboxService uploader = new DropboxService();
uploader.uploadFile("/Users/cem/Desktop/test.txt");
}
}
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology