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文件流的 断点下载
本文详细介绍了如何使用Java实现HTTP文件流的断点下载功能,通过设置断点续传的开始位置并利用随机访问文件操作来实现前100字节的下载,之后修改参数继续下载剩余部分,直至文件完整下载。

2万+

被折叠的 条评论
为什么被折叠?



