san*_*d17 6 java http download
我想用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文件.
有没有办法只下载文件的准备部分?我想如果这可以下载大文件(最多几GB).
Bac*_*ash 19
这应该工作,我测试它,它适用于我:
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();
            }
        }
    }
}
如果你想让它在后台运行,只需在一个线程中调用它:
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
| 归档时间: | 
 | 
| 查看次数: | 17256 次 | 
| 最近记录: |