0

I have tried this below code which is working fine with the small size files.

URL url = new URL(downloadLink);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(newFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();

If the file is a large file, it is giving an error like :

java.io.IOException:Server returned HTTP response code: 400 for URL:https://dl.dropboxusercontent.com/1/view/79q8i6f9zqx7y2w/Paragliding in Himalayas.avi

Can anyone help me.

java developer
  • 393
  • 2
  • 7
  • 20
  • How large is "large", and does the HTTP response contain any other useful information other than the 400 code? – jsheeran Nov 01 '18 at 10:54
  • 200MB file size. java.io.IOException:Server returned HTTP response code: 400 for URL:XXXXXX only this message was throwing – java developer Nov 01 '18 at 10:56
  • Possible duplicate of https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java – Sudhir Ojha Nov 01 '18 at 10:56
  • @SudhirOjhaI have tried all ways mentioned in that post. no luck – java developer Nov 01 '18 at 10:57
  • Look at here also https://stackoverflow.com/questions/25045003/how-to-download-large-sized-files-size-50mb-in-java – Sudhir Ojha Nov 01 '18 at 11:04
  • 2
    One interesting observation here is that the `400` status code would be sent before you start actually reading the full bytes of the file, indicating that the server is unhappy about some part of the request (e.g. headers that sent) hence a client error. Are you able to increase the log level of your application to show the full request headers being sent? – David Nov 01 '18 at 11:09
  • 1
    The URL you're trying to download is invalid. URLs can't contain spaces. You need to URL encode it: `https://dl.dropboxusercontent.com/1/view/79q8i6f9zqx7y2w/Paragliding%20in%20Himalayas.avi` Important note, you'll need to URL encode everything after the machine name, otherwise, it'll encode the protocol too. – Jason Armstrong Nov 01 '18 at 13:05
  • Have a look at: https://stackoverflow.com/questions/3292579/how-do-i-encode-a-complete-http-url-string-correctly, FWIW, I used your existing code, and got it to work fine by manually passing a hand-coded URL. – Jason Armstrong Nov 01 '18 at 13:13
  • 1
    @JasonArmstrong, thanks for your suggestion, it's working fine. If you post your suggestion as an answer I will accept it and do upvote for it. – java developer Nov 02 '18 at 10:07

2 Answers2

0

The URL you're trying to download is invalid. URLs can't contain spaces. You need to URL encode it:

https://dl.dropboxusercontent.com/1/view/79q8i6f9zqx7y2w/Paragliding%20in%20Himalayas.avi 

Have a look at: how do I encode a complete http url String correctly?, for an approach to encoding the URL. Unfortunately, just using URLEncoder.encode() won't cut it, since it encodes slashes, etc.

Jason Armstrong
  • 1,058
  • 9
  • 17
-1

You can hand this task over to Os itself by using DownloadManager

 private fun startDownload(url: String) {
    val request = DownloadManager.Request(Uri.parse(url))
    request.allowScanningByMediaScanner()
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url.substring(url.lastIndexOf("/")))
    val dm = activity!!.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
    dm.enqueue(request)
    Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show()
}
Vikash Bijarniya
  • 404
  • 4
  • 10