Java 断点续传

断点续传原理

静态Web服务器支持仅发送静态资源的指定范围数据, 请求头为 Range

$ telnet abc.com 80
HEAD /rap.flv HTTP/1.1
Host: a.c
RANGE: bytes=2000070-

HTTP/1.1 206 Partial Content
Server: nginx
Date: Sun, 02 Jun 2019 14:10:43 GMT
Content-Type: video/x-flv
Content-Length: 16404710
Last-Modified: Fri, 26 Apr 2019 07:18:55 GMT
Connection: keep-alive
ETag: "5cc2b0df-118d5ac"
developer: develon
原始uri: /rap.flv
Content-Range: bytes 2000070-18404779/18404780
HEAD /rap.flv HTTP/1.1
Host: a.c
RANGE: bytes=2000070-2000071

HTTP/1.1 206 Partial Content
Server: nginx
Date: Sun, 02 Jun 2019 14:11:43 GMT
Content-Type: video/x-flv
Content-Length: 2
Last-Modified: Fri, 26 Apr 2019 07:18:55 GMT
Connection: keep-alive
ETag: "5cc2b0df-118d5ac"
developer: develon
原始uri: /rap.flv
Content-Range: bytes 2000070-2000071/18404780
URL url = new URL("http://www.sjtu.edu.cn/down.zip"); 
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection(); 

// 设置 User-Agent 
httpConnection.setRequestProperty("User-Agent","NetFox"); 
// 设置断点续传的开始位置 
httpConnection.setRequestProperty("RANGE","bytes=2000070"); 
// 获得输入流 
InputStream input = httpConnection.getInputStream(); 

https://www.ibm.com/developerworks/cn/java/joy-down/index.html

你可能感兴趣的:(Java 断点续传)