Home >Java >javaTutorial >URL implementation of breakpoint download in Java

URL implementation of breakpoint download in Java

巴扎黑
巴扎黑Original
2017-05-21 14:26:361665browse

Java中 URL实现断点下载,需要的朋友可以参考一下

代码如下:

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

The above is the detailed content of URL implementation of breakpoint download in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn