我想用
java下载HTTP查询,但我下载的文件在下载时的长度不确定.
我认为这是非常标准的,所以我搜索并找到了它的代码片段:http://snipplr.com/view/33805/
但是它有一个contentLength变量的问题.由于长度未知,我得到-1回.这会产生错误.当我省略关于contentLength的整个检查时,这意味着我总是必须使用最大缓冲区.
但问题是该文件还没有准备好.因此,flush只会被部分填充,部分文件会丢失.
如果您尝试使用该片段下载类似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的链接,您会注意到该错误,并且当您总是下载最大缓冲区以省略错误时,您最终会得到一个损坏的XML文件.
解决方法
这应该工作,我测试它,它适用于我:
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,len); } } finally { try { if (is != null) { is.close(); } } finally { if (fos != null) { fos.close(); } } } }
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