Dropbox upload file java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Simple java command line application for uploading file(s) to Dropbox.

License

czpilar/dropdrive

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

  • dropdrive-cmd is simple java command line application for uploading file(s) to Dropbox.
  • dropdrive-core is core library which provide ability for uploading file(s) to Dropbox.

dropDrive as command line application

-a — process authorization
-d — directory for upload; creates new one if no directory exists; default is dropdrive-uploads
-f — upload files
-h — show this help
-l — display authorization link
-p — path to dropDrive properties file
-v -show dropDrive version

How to authorize application

  1. generate authorization URL:
    dropdrive -p dropdrive.properties -l
  2. copy and paste URL to your browser to receive authorization code
  3. authorize application with received authorization code:
    dropdrive -p dropdrive.properties -a
Читайте также:  Vue typescript eslint prettier

Upload file(s) to Dropbox:
dropdrive -p dropdrive.properties -f

Files are uploaded to dropdrive-uploads directory by default.

If you want to change upload directory:

  • change dropdrive.uploadDir property in properties file
  • or pass directory in -d argument:
    dropdrive -p dropdrive.properties -f -d //

How to use properties file

  • dropdrive.accessToken — Dropbox access token; this property is updated automatically by dropDrive
  • dropdrive.uploadDir — path to dir where files will be uploaded: //

dropDrive as core library

dropDrive core can be used in any other application to provide ability for uploading file(s) to Dropbox.

  1. add implementation of IDropDriveCredential interface to spring context or use SimpleDropDriveCredential or extend AbstractDropDriveCredential
  2. provide client key as dropdrive.core.drive.clientKey property in spring context
  3. provide client secret as dropdrive.core.drive.clientSecret property in spring context
  4. import dropDrive spring context with or using annotation @Import(net.czpilar.dropdrive.core.context.DropDriveCoreContext.class)
  5. autowire IFileService and use file uploading methods
Copyright 2019 David Pilar Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

About

Simple java command line application for uploading file(s) to Dropbox.

Источник

Dropbox Java API Tutorial

This Java tutorial is to demonstrate the Dropbox Java API which can be used to manage the files in a Dropbox account via Java code. This is fairly a simple Java API and easy to use.

Java-Dropbox-API

Dropbox

Do we need an introduction for Dropbox? If you are thinking about what is Dropbox, its a really popular application to store and edit documents from computer, mobile and tablet. Dropbox is one of the successful YCombinator start-ups and now worth in billions. We get 2GB free when we signup and additional storage can be purchased. This can be effectively used as a backup space. Dropbox innovated and set this domain on fire. Now we have Google Drive, Microsoft OneDrive and so many players into this business.

Dropbox Java API

Dropbox provides API for almost all popular programming platforms. API provides programmatic way to read and write to Dropbox. Java Dropbox API can be used to interact with Dropbox from our Java applications.

Читайте также:  Php ascii hex to hex

Dropbox Java API Example

Following example illustrates how to write a Java application to upload a file to Dropbox, create folder, check the size and other information.

1. Create Dropbox App

We need to create an application in Dropbox. Login and go to the Dropbox Apps console.

Create-Dropbox-App

Dropbox-API-app

2. Dropbox App Secret and Dropbox App Key

We need the Dropbox App secret and Dropbox App key to authorize with the Dropbox API. Grab them as shown below.

Dropbox-App-Secret-App-Key

Dropbox API Authentication

As a prerequisite, we should create a Dropbox App and get the Drobox App Secret, App Key as detailed above. We should instantiate DbxAppInfo by passing the Dropbox App Secret and App key. Once instantiated, we can authorize using the web request using DbxWebAuthNoRedirect . We can get the authorize url and go to that URL and then allow access and get the auth code.

Allow Access

Dropbox-API-Allow-Auth

Get Access Code

Java-Dropbox-API-Auth-access-code

Use the above access code in Java application to gain access to the Dropbox API.

public DbxClient authDropbox(String dropBoxAppKey, String dropBoxAppSecret) throws IOException, DbxException < DbxAppInfo dbxAppInfo = new DbxAppInfo(dropBoxAppKey, dropBoxAppSecret); DbxRequestConfig dbxRequestConfig = new DbxRequestConfig( "JavaDropboxTutorial/1.0", Locale.getDefault().toString()); DbxWebAuthNoRedirect dbxWebAuthNoRedirect = new DbxWebAuthNoRedirect( dbxRequestConfig, dbxAppInfo); String authorizeUrl = dbxWebAuthNoRedirect.start(); System.out.println("1. Authorize: Go to URL and click Allow : " + authorizeUrl); System.out .println("2. Auth Code: Copy authorization code and input here "); String dropboxAuthCode = new BufferedReader(new InputStreamReader( System.in)).readLine().trim(); DbxAuthFinish authFinish = dbxWebAuthNoRedirect.finish(dropboxAuthCode); String authAccessToken = authFinish.accessToken; dbxClient = new DbxClient(dbxRequestConfig, authAccessToken); System.out.println("Dropbox Account Name: " + dbxClient.getAccountInfo().displayName); return dbxClient; >

Get Dropbox Size using Java

/* returns Dropbox size in GB */ public long getDropboxSize() throws DbxException < long dropboxSize = 0; DbxAccountInfo dbxAccountInfo = dbxClient.getAccountInfo(); // in GB :) dropboxSize = dbxAccountInfo.quota.total / 1024 / 1024 / 1024; return dropboxSize; >

Upload file to Dropbox using Java

public void uploadToDropbox(String fileName) throws DbxException, IOException < File inputFile = new File(fileName); FileInputStream fis = new FileInputStream(inputFile); try < DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName, DbxWriteMode.add(), inputFile.length(), fis); String sharedUrl = dbxClient.createShareableUrl("/" + fileName); System.out.println("Uploaded: " + uploadedFile.toString() + " URL " + sharedUrl); >finally < fis.close(); >>

File uploaded to Dropbox successfully.

Dropbox-File-Upload

Create folder in Dropbox using Java

public void createFolder(String folderName) throws DbxException < dbxClient.createFolder("/" + folderName); >

List files in Dropbox using Java

public void listDropboxFolders(String folderPath) throws DbxException < DbxEntry.WithChildren listing = dbxClient .getMetadataWithChildren(folderPath); System.out.println("Files List:"); for (DbxEntry child : listing.children) < System.out.println(" " + child.name + ": " + child.toString()); >>

Download file from Dropbox using Java

public void downloadFromDropbox(String fileName) throws DbxException, IOException < FileOutputStream outputStream = new FileOutputStream(fileName); try < DbxEntry.File downloadedFile = dbxClient.getFile("/" + fileName, null, outputStream); System.out.println("Metadata: " + downloadedFile.toString()); >finally < outputStream.close(); >>

Complete Java Dropbox API Example

package com.javapapers.java.dropbox; 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.DbxAccountInfo; import com.dropbox.core.DbxAppInfo; import com.dropbox.core.DbxAuthFinish; import com.dropbox.core.DbxClient; import com.dropbox.core.DbxEntry; import com.dropbox.core.DbxException; import com.dropbox.core.DbxRequestConfig; import com.dropbox.core.DbxWebAuthNoRedirect; import com.dropbox.core.DbxWriteMode; public class JavaDropbox < private static final String DROP_BOX_APP_KEY = "bxsicd65ljq0ymh"; private static final String DROP_BOX_APP_SECRET = "3t1lqvq1amaehb1"; DbxClient dbxClient; public DbxClient authDropbox(String dropBoxAppKey, String dropBoxAppSecret) throws IOException, DbxException < DbxAppInfo dbxAppInfo = new DbxAppInfo(dropBoxAppKey, dropBoxAppSecret); DbxRequestConfig dbxRequestConfig = new DbxRequestConfig( "JavaDropboxTutorial/1.0", Locale.getDefault().toString()); DbxWebAuthNoRedirect dbxWebAuthNoRedirect = new DbxWebAuthNoRedirect( dbxRequestConfig, dbxAppInfo); String authorizeUrl = dbxWebAuthNoRedirect.start(); System.out.println("1. Authorize: Go to URL and click Allow : " + authorizeUrl); System.out .println("2. Auth Code: Copy authorization code and input here "); String dropboxAuthCode = new BufferedReader(new InputStreamReader( System.in)).readLine().trim(); DbxAuthFinish authFinish = dbxWebAuthNoRedirect.finish(dropboxAuthCode); String authAccessToken = authFinish.accessToken; dbxClient = new DbxClient(dbxRequestConfig, authAccessToken); System.out.println("Dropbox Account Name: " + dbxClient.getAccountInfo().displayName); return dbxClient; >/* returns Dropbox size in GB */ public long getDropboxSize() throws DbxException < long dropboxSize = 0; DbxAccountInfo dbxAccountInfo = dbxClient.getAccountInfo(); // in GB :) dropboxSize = dbxAccountInfo.quota.total / 1024 / 1024 / 1024; return dropboxSize; >public void uploadToDropbox(String fileName) throws DbxException, IOException < File inputFile = new File(fileName); FileInputStream fis = new FileInputStream(inputFile); try < DbxEntry.File uploadedFile = dbxClient.uploadFile("/" + fileName, DbxWriteMode.add(), inputFile.length(), fis); String sharedUrl = dbxClient.createShareableUrl("/" + fileName); System.out.println("Uploaded: " + uploadedFile.toString() + " URL " + sharedUrl); >finally < fis.close(); >> public void createFolder(String folderName) throws DbxException < dbxClient.createFolder("/" + folderName); >public void listDropboxFolders(String folderPath) throws DbxException < DbxEntry.WithChildren listing = dbxClient .getMetadataWithChildren(folderPath); System.out.println("Files List:"); for (DbxEntry child : listing.children) < System.out.println(" " + child.name + ": " + child.toString()); >> public void downloadFromDropbox(String fileName) throws DbxException, IOException < FileOutputStream outputStream = new FileOutputStream(fileName); try < DbxEntry.File downloadedFile = dbxClient.getFile("/" + fileName, null, outputStream); System.out.println("Metadata: " + downloadedFile.toString()); >finally < outputStream.close(); >> public static void main(String[] args) throws IOException, DbxException < JavaDropbox javaDropbox = new JavaDropbox(); javaDropbox.authDropbox(DROP_BOX_APP_KEY, DROP_BOX_APP_SECRET); System.out.println("Dropbox Size: " + javaDropbox.getDropboxSize() + " GB"); javaDropbox.uploadToDropbox("happy.png"); javaDropbox.createFolder("tutorial"); javaDropbox.listDropboxFolders("/"); javaDropbox.downloadFromDropbox("happy.png"); >>

Dropbox API Example Output

1. Authorize: Go to URL and click Allow : https://www.dropbox.com/1/oauth2/authorize?locale=en_IN&client_id=bxsicd65ljq0ymh&response_type=code 2. Auth Code: Copy authorization code and input here _-heFn6Qtl8AAAAAAAALAEtTxFPQPHNI-T0PufKYbv0 Dropbox Account Name: Joseph Kulandai R Dropbox Size: 2 GB Uploaded: File("/happy (4).png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 13:02:12 UTC", clientMtime="2014/12/07 13:02:12 UTC", rev="82e10806e") URL https://www.dropbox.com/s/nq6ebu8mqbfr6pz/happy.png?dl=0 Files List: happy (1).png: File("/happy (1).png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 12:40:07 UTC", clientMtime="2014/12/07 12:40:07 UTC", rev="42e10806e") happy (2).png: File("/happy (2).png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 12:58:50 UTC", clientMtime="2014/12/07 12:58:50 UTC", rev="52e10806e") happy (3).png: File("/happy (3).png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 13:00:23 UTC", clientMtime="2014/12/07 13:00:24 UTC", rev="62e10806e") happy (4).png: File("/happy (4).png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 13:02:12 UTC", clientMtime="2014/12/07 13:02:12 UTC", rev="82e10806e") happy.png: File("/happy.png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 12:07:13 UTC", clientMtime="2014/12/07 12:07:14 UTC", rev="22e10806e") tutorial: Folder("/tutorial", iconName="folder", mightHaveThumbnail=false) Metadata: File("/happy.png", iconName="page_white_picture", mightHaveThumbnail=true, numBytes=24670, humanSize="24.1 KB", lastModified="2014/12/07 12:07:13 UTC", clientMtime="2014/12/07 12:07:14 UTC", rev="22e10806e")

Download Dropbox Java API Example

Источник

Оцените статью