命令行:
如果您尝试在没有 IDE 的情况下构建它并使用 Dropbox Site 中包含的 Main.java 文件...
// Include the Dropbox SDK.
import com.dropbox.core.*;
import java.io.*;
import java.util.Locale;
public class Main
{
public static void main(String[] args) throws IOException, DbxException
{
// Get your app key and secret from the Dropbox developers website.
final String APP_KEY = "INSERT_APP_KEY";
final String APP_SECRET = "INSERT_APP_SECRET";
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);
DbxClient client = new DbxClient(config, authFinish.accessToken);
System.out.println("Linked account: " + client.getAccountInfo().displayName);
File inputFile = new File("working-draft.txt");
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());
}
finally
{
inputStream.close();
}
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());
}
FileOutputStream outputStream = new FileOutputStream("magnum-opus.txt");
try
{
DbxEntry.File downloadedFile = client.getFile("/magnum-opus.txt", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
}
finally
{
outputStream.close();
}
}
}
从适当的目录在终端中使用这些命令行:
编译:javac -cp dropbox-core-sdk-1.7.5.jar Main.java
运行:java -cp .;dropbox-core-sdk-1.7.5.jar;jackson-core-2.2.3.jar Main(对于类 Unix 操作系统,使用 : 而不是 ;)
Eclipse IDE:
如果您使用Eclipse IDE,您只需将dropbox-core-sdk-1.7.5.jar 文件复制到您的项目中,右键单击它,然后选择菜单选项Build Path -> Add to Build Path。然后它应该会出现在Package Explorer 窗口中的Referenced Libraries 文件夹中,然后您应该能够导入和使用它。
对jackson-core-2.2.3.jar重复该过程。
如果您愿意,此链接有多个分步图像,展示了将.jar 文件添加到您的项目的一般过程:http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
// Import whatever you want from the Dropbox SDK...
import com.dropbox.*; // everything
import com.dropbox.core.*;
import com.dropbox.core.http.*;
import com.dropbox.core.json.*;
import com.dropbox.core.util.*;
public class Test
{
public static void main(String[] args)
{
// Do stuff
}
}