欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中 URL实现断点下载

程序员文章站 2023-11-27 09:21:22
复制代码 代码如下: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();