public class TestHttpResume {
public static void main(String[] args) throws Exception {
BufferedOutputStream output = null;
BufferedInputStream br = null;
URL url = new URL("http://IP/test.war");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); // 设置 User-Agent
httpConnection.setRequestProperty("User-Agent", "NetFox"); // 设置断点续传的开始位置
httpConnection.setRequestProperty("RANGE", "bytes=0-100"); // 获得输入流
InputStream input = httpConnection.getInputStream();
RandomAccessFile file = new RandomAccessFile(new File("D://lms3.war"), "rw");
long nPos = 0;
// 定位文件指针到 nPos 位置
file.seek(0);
byte[] b = new byte[1024];
int nRead; // 从输入流中读入字节流,然后写到文件中
while ((nRead = input.read(b)) > 0) {
file.write(b, 0, nRead);
}
}
}
实现前100 的 下载
之后修改httpConnection.setRequestProperty("RANGE", "bytes=0-100"); // 获得输入流
为100-
文件定位到 file.seek(100);
至此文件完结
实际可动态修改 参数 实现http文件流的 断点下载