使用Java通过HTTP下载文件,其长度未知 [英] Download file via HTTP with unknown length with Java

查看:75
本文介绍了使用Java通过HTTP下载文件,其长度未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java下载HTTP查询,但是下载时下载的文件长度不确定。

I want to download a HTTP query with java, but the file I download has an undetermined length when downloading.

我认为这是相当标准的,所以我搜索并找到了相应的代码段: http://snipplr.com/view/33805/

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

但是contentLength变量存在问题。由于长度未知,我得到-1。这会产生一个错误。当我省略对contentLength的整个检查时,这意味着我总是必须使用最大缓冲区。

But it has a problem with the contentLength variable. As the length is unknown, I get -1 back. This creates an error. When I omit the entire check about contentLength, that means I always have to use the maximum buffer.

但是问题是文件尚未准备好。因此刷新只会部分填充,文件的某些部分也会丢失。

But the problem is that the file is not ready yet. So the flush gets only partially filled, and parts of the file get lost.

如果您尝试下载类似 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文件损坏。

If you try downloading a link like 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 that snippet, you'll notice the error, and when you always download the maximum buffer to omit the error, you end up with a corrupt XML file.

是否有某种方法只能下载文件的就绪部分?我希望这是否可以下载大文件(最大几个GB)。

Is there some way to only download the ready part of the file? I would like if this could download big files (up to a few GB).

推荐答案

这应该可以工作,我已经对其进行了测试并它对我有用:

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 background, simply call it in a 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

这篇关于使用Java通过HTTP下载文件,其长度未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆