Download file over HTTP with unknown length using Java

I want to download an HTTP request using java, but the download file has an undefined length when loading.

I thought it would be quite standard, so I searched and found a code snippet for it: http://snipplr.com/view/33805/

But it has a problem with the variable contentLength. Since the length is unknown, I get -1 back. This creates an error. When I omit all contentLength content validation, it means that I should always use the maximum buffer.

But the problem is that the file is not ready yet. Thus, the flash is only partially filled, and parts of the file are lost.

If you try to download a link, for example http://overpass-api.de/api/interpreter?data=area%5Bname%3D%22Hoogstade%22%5D%3B%0A%28%0A++node%28area%29% 3B% 0A ++% 3C% 3B% 0A% 29 +% 3B% 0Aout + meta + qt% 3B with this fragment, you will notice an error and will always load the maximum buffer to omit the error, you will get a damaged XML file.

Is there a way to download only the finished part of the file? I would like it to be able to upload large files (up to a few GB).

+6
source share
1 answer

This should work, I tested it and it works for me:

void downloadFromUrl(URL url, String localFilename) throws IOException { InputStream is = null; FileOutputStream fos = null; try { URLConnection urlConn = url.openConnection();//connect is = urlConn.getInputStream(); //get connection inputstream fos = new FileOutputStream(localFilename); //open outputstream to local file byte[] buffer = new byte[4096]; //declare 4KB buffer int len; //while we have availble data, continue downloading and storing to local file while ((len = is.read(buffer)) > 0) { fos.write(buffer, 0, len); } } finally { try { if (is != null) { is.close(); } } finally { if (fos != null) { fos.close(); } } } } 

If you want this to run in the background, just call it in the thread:

 Thread download = new Thread(){ public void run(){ URL url= new URL("http://overpass-api.de/api/interpreter?data=area%5Bname%3D%22Hoogstade%22%5D%3B%0A%28%0A++node%28area%29%3B%0A++%3C%3B%0A%29+%3B%0Aout+meta+qt%3B"); String localFilename="mylocalfile"; //needs to be replaced with local file path downloadFromUrl(url, localFilename); } }; download.start();//start the thread 
+19
source

All Articles