java - Can't download file from dropbox -
i've got public file storage @ dropbox , want download using java. how did:
string url = "http://www.dropbox.com/s/vk67dz9ca0oqz37/chrysanthemum.jpg"; string filename = "c:\\users\\public\\pictures\\sample pictures\\test.jpg"; 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(); }
but test.jpg
invalid. wrong?
when click on "download original" in dropbox page, can see redirects https://www.dropbox.com/s/vk67dz9ca0oqz37/chrysanthemum.jpg?dl=1
so, append ?dl=1
url , use https
.
string url = "https://www.dropbox.com/s/vk67dz9ca0oqz37/chrysanthemum.jpg?dl=1"; string filename = "c:\\users\\public\\pictures\\sample pictures\\test.jpg"; 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(); }
or, shorter:
string url = "https://www.dropbox.com/s/vk67dz9ca0oqz37/chrysanthemum.jpg?dl=1"; string filename = "c:\\users\\public\\pictures\\sample pictures\\test.jpg"; try { url download = new url(url); path fileout = new file(filename).topath(); files.copy(download.openstream(), fileout, standardcopyoption.replace_existing); } catch (exception e) { e.printstacktrace(); }
Comments
Post a Comment