Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading file from dropbox in java

I'm writing a swing application, but I'm sure I'll think of more to add to it later, so I would like a way to download the file from dropbox if its new. I've tried a lot of different things, but all they give me are the page's HTML. Anyone know how to do this? I sure don't.

like image 846
TheNerdyCoder Avatar asked Oct 05 '22 20:10

TheNerdyCoder


1 Answers

In my opinion, the Dropbox API is far too complicated for what you need. It's actually extremely simple to download a file from dropbox.

The first step is to put the file that you want to download somewhere inside your dropbox's Public Folder.

Next you want to right click that file and choose "copy public link." You can do this from the web interface or even right there in your computer-sync-folder-thing. This will give you a unique download url for the file.

Next, use this code:

String url="https://dl.dropboxusercontent.com/u/73386806/Prune%20Juice/Prune%20Juice.exe";
String filename="PruneJuice.exe";

try{
    URL download=new URL(url);
    ReadableByteChannel rbc=Channels.newChannel(download.openStream());
    FileOutputStream fileOut = new FileOutputStream(filename);
    fileOut.getChannel().transferFrom(rbc, 0, 1 << 24);
    fileOut.flush();
    fileOut.close();
    rbc.close();
}catch(Exception e){ e.printStackTrace(); }

Of course, change the value of the url string to your own download url, and the value of filename to whatever you want to save the file as.

Now, if this fails, you may need to change the url from https:// to http://, but either way it should still work. Dropbox is cool like that.

like image 82
Daniel K Avatar answered Oct 10 '22 03:10

Daniel K