URL url = new URL(" http://www.sjtu.edu.cn/down.zip";;); HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection (); //設置User-Agent httpConnection.setRequestProperty("User-Agent","NetFox"); //設置斷點續傳的開始位置 httpConnection.setRequestProperty("RANGE","bytes=2000070"); //得到輸入流 InputStream input = httpConnection.getInputStream(); |
RandomAccess oSavedFile = new RandomAccessFile("down.zip","rw"); long nPos = 2000070; //定位文件指針到nPos位置 oSavedFile.seek(nPos); byte[] b = new byte[1024]; int nRead; //從輸入流中讀入字節流,而後寫到文件中 while((nRead=input.read(b,0,1024)) > 0) { oSavedFile.write(b,0,nRead); } |