複製代碼代碼如下:
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();