![](https://img-blog.csdn.net/20140418095811625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzkyMTkwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
假设我们在C盘上有一个a.txt文件已经下载了5个字符,要下载剩下的所有字符串
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Range {
public static void main(String[] args) throws IOException{
URL url = new URL("http://localhost:80/news/a.txt");
//建立连接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置断点续传,续传5以后的
conn.setRequestProperty("Range", "bytes=5-");
InputStream in = conn.getInputStream();
FileOutputStream out = new FileOutputStream("c:\\a.txt",true);
int len = 0;
byte[] buf = new byte[1024];
while((len=in.read(buf))>0)
{
out.write(buf,0,len);
}
out.close();
}
}