Search in sources :

Example 1 with UploadErrorException

use of com.dropbox.core.v2.files.UploadErrorException in project dropbox-sdk-java by dropbox.

the class Main method uploadFile.

/**
 * Uploads a file in a single request. This approach is preferred for small files since it
 * eliminates unnecessary round-trips to the servers.
 *
 * @param dbxClient Dropbox user authenticated client
 * @param localFIle local file to upload
 * @param dropboxPath Where to upload the file to within Dropbox
 */
private static void uploadFile(DbxClientV2 dbxClient, File localFile, String dropboxPath) {
    try (InputStream in = new FileInputStream(localFile)) {
        ProgressListener progressListener = l -> printProgress(l, localFile.length());
        FileMetadata metadata = dbxClient.files().uploadBuilder(dropboxPath).withMode(WriteMode.ADD).withClientModified(new Date(localFile.lastModified())).uploadAndFinish(in, progressListener);
        System.out.println(metadata.toStringMultiline());
    } catch (UploadErrorException ex) {
        System.err.println("Error uploading to Dropbox: " + ex.getMessage());
        System.exit(1);
    } catch (DbxException ex) {
        System.err.println("Error uploading to Dropbox: " + ex.getMessage());
        System.exit(1);
    } catch (IOException ex) {
        System.err.println("Error reading from file \"" + localFile + "\": " + ex.getMessage());
        System.exit(1);
    }
}
Also used : NetworkIOException(com.dropbox.core.NetworkIOException) RetryException(com.dropbox.core.RetryException) DbxWebAuth(com.dropbox.core.DbxWebAuth) UploadSessionFinishErrorException(com.dropbox.core.v2.files.UploadSessionFinishErrorException) DbxClientV2(com.dropbox.core.v2.DbxClientV2) UploadSessionCursor(com.dropbox.core.v2.files.UploadSessionCursor) Date(java.util.Date) ProgressListener(com.dropbox.core.util.IOUtil.ProgressListener) WriteMode(com.dropbox.core.v2.files.WriteMode) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JsonReader(com.dropbox.core.json.JsonReader) Logger(java.util.logging.Logger) File(java.io.File) Level(java.util.logging.Level) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxException(com.dropbox.core.DbxException) CommitInfo(com.dropbox.core.v2.files.CommitInfo) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) UploadSessionLookupErrorException(com.dropbox.core.v2.files.UploadSessionLookupErrorException) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) UploadErrorException(com.dropbox.core.v2.files.UploadErrorException) DbxPathV2(com.dropbox.core.v2.DbxPathV2) InputStream(java.io.InputStream) ProgressListener(com.dropbox.core.util.IOUtil.ProgressListener) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UploadErrorException(com.dropbox.core.v2.files.UploadErrorException) FileMetadata(com.dropbox.core.v2.files.FileMetadata) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) DbxException(com.dropbox.core.DbxException)

Aggregations

DbxAuthInfo (com.dropbox.core.DbxAuthInfo)1 DbxException (com.dropbox.core.DbxException)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 DbxWebAuth (com.dropbox.core.DbxWebAuth)1 NetworkIOException (com.dropbox.core.NetworkIOException)1 RetryException (com.dropbox.core.RetryException)1 JsonReader (com.dropbox.core.json.JsonReader)1 ProgressListener (com.dropbox.core.util.IOUtil.ProgressListener)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 DbxPathV2 (com.dropbox.core.v2.DbxPathV2)1 CommitInfo (com.dropbox.core.v2.files.CommitInfo)1 FileMetadata (com.dropbox.core.v2.files.FileMetadata)1 UploadErrorException (com.dropbox.core.v2.files.UploadErrorException)1 UploadSessionCursor (com.dropbox.core.v2.files.UploadSessionCursor)1 UploadSessionFinishErrorException (com.dropbox.core.v2.files.UploadSessionFinishErrorException)1 UploadSessionLookupErrorException (com.dropbox.core.v2.files.UploadSessionLookupErrorException)1 WriteMode (com.dropbox.core.v2.files.WriteMode)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1