Reputation: 201
I want to download a file from my dropbox account using java DropBox API. I have tried using this code but this is displaying the list of files and folder while i want to download files to my system how it is possible
Here's my code:-
Scanner tokenScanner = new Scanner(tokensFile);
String ACCESS_TOKEN_KEY = tokenScanner.nextLine(); // Read key
String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
tokenScanner.close(); //Close Scanner
//Re-auth
AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET);
mDBApi.getSession().setAccessTokenPair(reAuthTokens);
Entry entries = mDBApi.metadata("/", 20, null, true, null);
for (Entry e: entries.contents) {
if(!e.isDeleted){
if(e.isDir){
System.out.println("Folder ---> " + e.fileName() );
} else {
// this will download the root level files.
System.out.println("File ---->" + e.fileName());
DropboxInputStream inputStream = mDBApi.getFileStream(e.path,null);
OutputStream out=new FileOutputStream(e.fileName());
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
out.close();
inputStream.close();
System.out.println("File is created....");
Upvotes: 5
Views: 4799
Reputation:
This is a basic example for downloading a file that work with dropbox. it does not include an progress of file download, this is just for straight forward file download.
This example uses dropbox API v2 using DbxClientV2
try
{
//output file for download --> storage location on local system to download file
OutputStream downloadFile = new FileOutputStream("C:\\.....");
try
{
FileMetadata metadata = client.files().downloadBuilder("/root or foldername here/Koala.jpg")
.download(downloadFile);
}
finally
{
downloadFile.close();
}
}
//exception handled
catch (DbxException e)
{
//error downloading file
JOptionPane.showMessageDialog(null, "Unable to download file to local system\n Error: " + e);
}
catch (IOException e)
{
//error downloading file
JOptionPane.showMessageDialog(null, "Unable to download file to local system\n Error: " + e);
}
Hope this helps and use this example and edit it to work the way you want it to work.
Upvotes: 9