您的位置 首页 java

HTTP断点续传

HTTP断点续传原理

一般通过Http下载的时候,我们发送一个request,服务器返回一个response,我们可以读取里面InputStream流,然后保存为文件。

比如我们请求一个下载地址:

发送的HTTP Request Headers

:authority: storage.360buyimg.com
:method: GET
:path: /jdmobile/JDMALL-PC2.apk
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
 Cache-Control : no-cache
pragma: no-cache
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
 

返回的Http Response Headers:

accept- RANGE s: bytes
access-control-allow-origin: *
age: 1045106
cache-control: max-age=315360000
content-length: 87819000
content-type: application/octet-stream
date: Sun, 19 May 2019 10:43:56 GMT
 ETag : "98ccc64969643f58484cff68bad440c2"
 expires : Thu, 31 Dec 2037 23:55:55 GMT
last-modified: Tue, 07 May 2019 08:25:23 GMT
server: JDWS/2.0
status: 200
via: http/1.1 BJ-Y-JCS-203 ( [cHs f ]), http/1.1 HB-CM-1-PIX-144 ( [cHs f ])
x-jss-request-id: BCEACE72D5E2E54B
 

从上面的response headers可以获取到,服务器接收断点续传(accept-ranges: bytes

),并且获取到了文件的大小87819000 bytes(content-length: 87819000)。

如果服务器返回response headers 中包含accept-ranges: bytes,说明服务器支持断点续传的。

所谓断点续传,也就是要从文件已经下载的地方开始继续下载。怎么添加开始的地方?

其实在request headers添加以下header即可:

RANGE: bytes=10000000- 
//或者
//RANGE: bytes=10000000- 20000000
 

服务器返回的大小是从开始的位置的大小,77819000 bytes(content-length: 77819000)。 返回的状态码不是200,而是206。

HTTP/1.1 206 Partial Content
Server:JDWS/2.0
Access-Control-Allow-Origin:*
Content-Range:bytes 17563800-87818999/87819000
x-jss-request-id:BCEACE72D5E2E54B
Connection:keep-alive
Last-Modified:Tue, 07 May 2019 08:25:23 GMT
Date:Sun, 19 May 2019 10:27:08 GMT
Via:http/1.1 BJ-Y-JCS-203 ( cHs f ), http/1.1 HB-CM-1-PIX-144 ( cHs f )
Accept-Ranges:bytes
Cache-Control:max-age=315360000
ETag:"98ccc64969643f58484cff68bad440c2"
Expires:Thu, 31 Dec 2037 23:55:55 GMT
Content-Length:70255200
Age:1044098
Content-Type:application/octet-stream
 

返回的Content-Range就是这次request的开始和结束位置(Content-Range:bytes 17563800-87818999/87819000)。

这样就可以实现断点续传了,后面会为大家实现一个多线程的断点续传。

刚开始写作,欢迎关注,一起进步,谢谢!

文章来源:智云一二三科技

文章标题:HTTP断点续传

文章地址:https://www.zhihuclub.com/177908.shtml

关于作者: 智云科技

热门文章

评论已关闭

3条评论

  1. Variable degrees of resistance of these isolates to all antibiotics tested were observed 6 Sputum 1 g IV 8 1 hr 9 Cerebrospinal fluid 2 g q8hrIV 5 120 min 9

  2. show that significant hepatic fibrosis in uniformly present following Fontan surgery, and that it correlates to time following the operation but not to abnormal liver enzymes, inferior vena cava pressure, ventricular morphology or severity of atrioventricular valve insufficiency Winer KK, Sinaii N, Reynolds J, et al Long term treatment of 12 children with chronic hypoparathyroidism a randomized trial comparing synthetic human parathyroid hormone 1 34 versus calcitriol and calcium

  3. Because these therapies remain the cornerstone of treatment for these types of malignancies, the problem of end stage HF from CCMP is likely to continue to increase in the future

网站地图