METHOD 1
USING ACCESS TOKEN GENERATED IN DROPBOX DEVELOPER WEBSITE
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.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;
public class Upload_Data
{
private static final String ACCESS_TOKEN = "<ACCESS TOKEN>";
public static void main(String args[]) throws DbxException, IOException {
DbxRequestConfig config = new DbxRequestConfig(
"JavaTutorial/1.0", Locale.getDefault().toString());
//YOU CAN GET THIS ACCESS TOKEN FROM DROPBOX DEVELOPER WEBSITE FOR YOUR APP
String accessToken="<aCCESSTOKEN>";
DbxClient client = new DbxClient(config, accessToken);
System.out.println("Linked account: " +client.getAccountInfo().displayName);
File inputFile = new File("C:\\Users\\USER\\Desktop\\dat.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try
{
client.delete("/dat.txt");
System.out.println("DAT DELETED SUCCESSFULLY");
DbxEntry.File uploadedFile = client.uploadFile("/dat.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
}
finally
{
inputStream.close();
}
FileOutputStream outputStream = new FileOutputStream("DAT.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/DAT.txt", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
}
finally
{
outputStream.close();
}
}
}
METHOD 2
USING ACCESS KEY AND SECRET KEY GENERATED IN DROPBOX DEVELOPER WEBSITE:
USING ACCESS KEY AND SECRET KEY GENERATED IN DROPBOX DEVELOPER WEBSITE:
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.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;
public class Upload_Data
{
private static final String ACCESS_TOKEN = "<ACCESS TOKEN>";
public static void main(String args[]) throws DbxException, IOException {
// Create Dropbox client
//YOU CAN GET APP KEY AND SECRET KEY FOR YOUR APP IN DROPBOX DEVELOPER.COM
final String APP_KEY = "<APP KEY>";
final String APP_SECRET = "SECRET KEY";
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig(
"JavaTutorial/1.0", Locale.getDefault().toString());
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
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();
System.out.println("Authen code.."+code);
DbxAuthFinish authFinish = webAuth.finish(code);
String accessToken = authFinish.accessToken;
DbxClient client = new DbxClient(config, accessToken);
System.out.println("Linked account: " +client.getAccountInfo().displayName);
File inputFile = new File("C:\\Users\\USER\\Desktop\\dat.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try
{
client.delete("/dat.txt");
System.out.println("DAT DELETED SUCCESSFULLY");
DbxEntry.File uploadedFile = client.uploadFile("/dat.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
}
finally
{
inputStream.close();
}
FileOutputStream outputStream = new FileOutputStream("DAT.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/DAT.txt", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
}
finally
{
outputStream.close();
}
}
}
No comments:
Post a Comment