URL implementation breakpoint download in Java

  • 2020-04-01 01:33:33
  • OfStack


URL ur = new URL("http://localhost:8080/first/he.txt");
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();//URL. The openConnection () -> Return URLCommection(direct subclass HttpURLConnection)
conn.setRequestProperty("Range", "bytes=5-");//Set the request parameter property, set the download from the fifth byte to download;
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();         


Related articles: