如果无法获得“内容长度”,如何使用多线程下载

时间:2021-03-24 19:31:06

I have learned that I can download a part of a file if I send the Range: bytes=n-m header to an HTTP server.

我了解到,如果我将Range: bytes=n-m报头发送到HTTP服务器,我可以下载文件的一部分。

Does that mean I can use multiple threads to download only if I know the exact file length? What I'm confused about is, how can I write a multi-threaded program, if I can't get the Content-Length beforehand?

这是否意味着只有知道确切的文件长度,我才能使用多个线程来下载?我困惑的是,如果我不能预先得到内容长度,我如何编写一个多线程程序?

1 个解决方案

#1


3  

Sending Range requests is only possible if the response on a full request (which can be a HEAD if you just want to check the headers) to that file returns a Accept-Ranges response header with value of bytes and a Content-Range or Content-Length header which both contain information about the content length (and also ETag and/or Last-Modified so that the file can be uniquely identified).

只是发送请求范围可能在一个完整的请求的响应(可一头如果你只想检查标头),文件返回Accept-Ranges响应头的值的字节内容长度和含量或标题都包含的信息内容的长度(也是ETag和/或最后修改的文件可以唯一标识)。

If there's no Accept-Ranges response header at all, then you're lost. It means that the server simply doesn't support it.

如果根本没有接受范围响应头,那么就会丢失。这意味着服务器根本不支持它。

#1


3  

Sending Range requests is only possible if the response on a full request (which can be a HEAD if you just want to check the headers) to that file returns a Accept-Ranges response header with value of bytes and a Content-Range or Content-Length header which both contain information about the content length (and also ETag and/or Last-Modified so that the file can be uniquely identified).

只是发送请求范围可能在一个完整的请求的响应(可一头如果你只想检查标头),文件返回Accept-Ranges响应头的值的字节内容长度和含量或标题都包含的信息内容的长度(也是ETag和/或最后修改的文件可以唯一标识)。

If there's no Accept-Ranges response header at all, then you're lost. It means that the server simply doesn't support it.

如果根本没有接受范围响应头,那么就会丢失。这意味着服务器根本不支持它。