Java中 URL实现断点下载
程序员文章站
2023-12-12 18:12:46
复制代码 代码如下:url ur = new url("http://localhost:8080/first/he.txt");httpurlconnection con...
复制代码 代码如下:
url ur = new url("http://localhost:8080/first/he.txt");
httpurlconnection conn = (httpurlconnection) ur.openconnection();//url.openconnection() -- >return urlcommection(直接子类httpurlconnection)
conn.setrequestproperty("range", "bytes=5-");//设置请求参数属性,设置下载从第5个字节开始下载;
inputstream in = conn.getinputstream();
int len = 0;
byte[] buf = new byte[1024];
fileoutputstream out = new fileoutputstream("d:\\a.txt");
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();