URL url = new URL(http://www.oschina.net/no-exist.zip);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.setRequestProperty("User-Agent","NetFox");
httpConnection.setRequestProperty("RANGE","bytes=2000070");
InputStream input = httpConnection.getInputStream();
从输入流中取出的字节流就是no-exist.zip文件从2000070开始的字节流。
大家看,其实断点续传用Java实现起来还是很简单的吧。
我采用的是IO包中的RandAccessFile类。操作相当简单,假设从2000070处开始保存文件,代码如下:
RandomAccessFile oSavedFile = new RandomAccessFile("down.zip","rw");
byte[] b = new byte[1024];
while((nRead=input.read(b,0,1024)) > 0)
oSavedFile.write(b,0,nRead);
转自:http://www.oschina.net/code/snippet_54100_623