Java中 URL实现断点下载
Java中 URL实现断点下载
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:URLur=newURL("http://localhost:8080/first/he.txt");HttpURLCon...

复制代码 代码如下:

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();

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Java学习
热门Java学习
编程开发子分类