当前位置: 编程技术>HTML标签参考手册
iis7站长之家
Java中 URL实现断点下载
来源: 互联网 发布时间:2014-10-23
本文导语: 代码如下: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-");//设置请求参数属性,设置下载...
代码如下:
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();