使用 http 协议查找(跳过)输入流的最快方法

发布于 2024-10-14 15:16:54 字数 605 浏览 5 评论 0原文

我正在制作某种下载服务,它能够恢复之前的部分下载。我目前正在使用像这样的跳过方法,

                long skipped = 0;
                while (skipped < track.getCacheFile().length()){
                    skipped += is.skip(track.getCacheFile().length()-skipped);

                }

我刚刚做了一个测试,在输入流中跳过 45 mb 花了大约 57 秒。我很好奇某些本机代码是如何做到这一点的,例如,媒体播放器可以立即寻找远程流的任何部分。我意识到我无法访问相同的库,但是我可以实现类似的目标吗?

顺便说一句,那个测试是在 wifi 上进行的。在普通数据网络上显然要慢得多。

更新:非常简单(感谢下面)

if (track.getCacheFile().length() > 0){
    request.setHeader("Range","bytes="+track.getCacheFile().length()+"-");  
}

I am making a download service of sorts, and it has the ability to resume a previous partial download. I am currently using the skip method like this

                long skipped = 0;
                while (skipped < track.getCacheFile().length()){
                    skipped += is.skip(track.getCacheFile().length()-skipped);

                }

I just did a test and it took about 57 seconds seconds to skip 45 mb in an inputstream. I am curious how certain native code does this, for instance, the mediaplayer can seek to any part of a remote stream instantaneously. I realize that I do not have access to the same libraries, but can I achieve something similar.

btw, that test was on wifi. It is obviously much slower on normal data networks.

Update: very simple (thanks to below)

if (track.getCacheFile().length() > 0){
    request.setHeader("Range","bytes="+track.getCacheFile().length()+"-");  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦回梦里 2024-10-21 15:16:54

如果您使用 http 作为启动输入流的协议,您可以尝试 RANGE 标头。

看看这里:

http://www.west-wind.com/Weblog/帖子/244.aspx

If you are using http as a protocol to initiate your inputstream, you may try the RANGE header.

Take a look here :

http://www.west-wind.com/Weblog/posts/244.aspx

谁人与我共长歌 2024-10-21 15:16:54

跳过方法的问题是,即使跳过数据,您也必须读取数据,因此您需要接收它们。最好的解决方案可能是仅向服务器请求您想要的部分。

The problem with the skip method is that you have to read the data even if you skip them so you need to receive them. The best solution is probably to request the server only the part you want.

一瞬间的火花 2024-10-21 15:16:54

你可以这样做:

private InputStream getRemote(String url, long offset) {
    try {
        URLConnection cn = new URL( url ).openConnection();
        cn.setRequestProperty ("Range", "bytes="+offset+"-");
        cn.connect();
        length = cn.getContentLength();
        return cn.getInputStream();
    } catch (MalformedURLException e ) { e.printStackTrace();
    } catch (IOException e) { e.printStackTrace(); }

    return null;
}

然后当你需要跳过时,你实际上通过 HTTP 重新连接到新的偏移量。工作快速可靠,比使用输入流的跳过要好得多。

You can do it like this:

private InputStream getRemote(String url, long offset) {
    try {
        URLConnection cn = new URL( url ).openConnection();
        cn.setRequestProperty ("Range", "bytes="+offset+"-");
        cn.connect();
        length = cn.getContentLength();
        return cn.getInputStream();
    } catch (MalformedURLException e ) { e.printStackTrace();
    } catch (IOException e) { e.printStackTrace(); }

    return null;
}

Then when you need to skip, you actually do a reconnect via HTTP to the new offset. Works quick and reliable, much better than using the inputstream's skip.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文