原理:主要是使用HTTP协议中range属性来取得资源的部分内容,由于一般服务是不对外直接听过url访问的,一般都是通过ID,在servlet中输出byte[]来实现,所以要想实现蹲点续传一般要自己实现服务端和客户端,客户端保持文件的下载和上传文件状态,可以保存在本地或者数据库中。宰金鑫中断时保持中断状态,在进行续传时,首先读出文件的状态,然后设置range属性信息发送续传请求。服务器收到续传请求,读取range属性值,从文件中读取数据,发送到客户端。上传和下载的原理是一样的。
客户端:
/**
* 实现断点续传的客户端
* @author uj
*
*/
public class LoadClient {
public static void load(int start, int end) throws Exception {
String endpoint = "http://localhost:8080/RestDemo/LoadServlet?id=5";;;
URL url = new URL(endpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
connection.setRequestProperty("RANGE","bytes="+start+"-"+end);//在header中增加range属性
connection.connect();
System.out.println(connection.getResponseCode());
System.out.println(connection.getContentLength());
System.out.println(connection.getContentType());
InputStream is = (InputStream) connection.getContent();
String filename = co