|
| 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