Skip to content

Commit 7791a8f

Browse files
author
Kannan Goundan
committed
New example: "upload-file".
1 parent 952dbe6 commit 7791a8f

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<modules>
1515
<module>authorize</module>
1616
<module>account-info</module>
17+
<module>upload-file</module>
1718
<module>web-file-browser</module>
1819
</modules>
1920

examples/upload-file/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

examples/upload-file/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<name>Upload File Example (Dropbox Core API SDK)</name>
8+
<artifactId>examples-upload-file</artifactId>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.dropbox.core</groupId>
13+
<artifactId>examples</artifactId>
14+
<version>0-SNAPSHOT</version>
15+
<relativePath>../pom.xml</relativePath>
16+
</parent>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-dependency-plugin</artifactId>
23+
<executions>
24+
<execution>
25+
<phase>compile</phase>
26+
<goals>
27+
<goal>build-classpath</goal>
28+
</goals>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
</project>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.dropbox.core.examples.upload_file;
2+
3+
import com.dropbox.core.*;
4+
import com.dropbox.core.json.JsonReader;
5+
import com.dropbox.core.util.IOUtil;
6+
7+
import java.io.FileInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.Locale;
11+
import java.util.logging.Level;
12+
import java.util.logging.Logger;
13+
14+
/**
15+
* An example command-line application that runs through the web-based OAuth
16+
* flow (using {@link DbxWebAuth}).
17+
*/
18+
public class Main
19+
{
20+
public static void main(String[] args)
21+
throws IOException
22+
{
23+
int code = _main(args);
24+
System.exit(code);
25+
}
26+
27+
private static int _main(String[] args)
28+
throws IOException
29+
{
30+
// Only display important log messages.
31+
Logger.getLogger("").setLevel(Level.WARNING);
32+
33+
if (args.length == 0) {
34+
System.out.println("");
35+
System.out.println("Usage: COMMAND <auth-file> <local-path> <dropbox-path>");
36+
System.out.println("");
37+
System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
38+
System.out.println(" an authorized Dropbox API request. Generate this file using the \"authorize\"");
39+
System.out.println(" example program.");
40+
System.out.println("");
41+
System.out.println(" <local-path>: The path to a local file whose contents you want to upload.");
42+
System.out.println("");
43+
System.out.println(" <dropbox-path>: The path on Dropbox to save the file to.");
44+
System.out.println("");
45+
return 0;
46+
}
47+
48+
if (args.length != 3) {
49+
System.err.println("Expecting exactly 3 arguments, got " + args.length + ".");
50+
System.err.println("Run with no arguments for help.");
51+
return 1;
52+
}
53+
54+
String argAuthFile = args[0];
55+
String localPath = args[1];
56+
String dropboxPath = args[2];
57+
58+
// Read auth info file.
59+
DbxAuthInfo authInfo;
60+
try {
61+
authInfo = DbxAuthInfo.Reader.readFromFile(argAuthFile);
62+
}
63+
catch (JsonReader.FileLoadException ex) {
64+
System.err.println("Error loading <auth-file>: " + ex.getMessage());
65+
return 1;
66+
}
67+
68+
String pathError = DbxPath.findError(dropboxPath);
69+
if (pathError != null) {
70+
System.err.println("Invalid <dropbox-path>: " + pathError);
71+
return 1;
72+
}
73+
74+
// Create a DbxClient, which is what you use to make API calls.
75+
String userLocale = Locale.getDefault().toString();
76+
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-upload-file", userLocale);
77+
DbxClient dbxClient = new DbxClient(requestConfig, authInfo.accessToken, authInfo.host);
78+
79+
// Make the API call to upload the file.
80+
DbxEntry.File metadata;
81+
try {
82+
InputStream in = new FileInputStream(localPath);
83+
try {
84+
metadata = dbxClient.uploadFile(dropboxPath, DbxWriteMode.add(), -1, in);
85+
} catch (DbxException ex) {
86+
System.out.println("Error uploading to Dropbox: " + ex.getMessage());
87+
return 1;
88+
} finally {
89+
IOUtil.closeInput(in);
90+
}
91+
}
92+
catch (IOException ex) {
93+
System.out.println("Error reading from file \"" + localPath + "\": " + ex.getMessage());
94+
return 1;
95+
}
96+
97+
System.out.print(metadata.toStringMultiline());
98+
return 0;
99+
}
100+
}

0 commit comments

Comments
 (0)