1 //1、声明URL
2 String path="http://localhost:8080/day22_DownLoad/file/a.rmvb";
3 URL url=new URL(path);
4 //2、设置已下载文件
5 String savePath="d:\\a.rmvb";
6 File file=new File(savePath);
7 long size=file.length();//文件当前大小,刚开始时返回0
8 System.out.println(size);
9 //3、设置连接
10 HttpURLConnection conn= (HttpURLConnection) url.openConnection();
11 //4、设置访问类型
12 conn.setRequestMethod("GET");
13 //5、设置下载区间
14 conn.setRequestProperty("range","bytes="+size+"-");
15 conn.connect();
16 //6、状态码
17 int code=conn.getResponseCode();//断点是206
18 if(code==206)
19 {
20 InputStream in=conn.getInputStream();
21 int serviceSize=conn.getContentLength();
22 //必须使用
23 RandomAccessFile out=new RandomAccessFile(file, "rw");
24 //从size字节开始写
25 out.seek(size);
26 byte[] b=new byte[1024];
27 int len=-1;
28 while((len=in.read(b))!=-1)
29 {
30 out.write(b,0,len);
31 }
32 out.close();
33 }