Set the range and use randomaccessfile:
private void download() throws IOException {HttpURLConnection httpConn = null; httpConn = (HttpURLConnection) url.openConnection();httpConn.setRequestMethod("POST");httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");httpConn.setRequestProperty("Range", "bytes="+beginIndex+"-"+"endIndex");InputStream in = httpConn.getInputStream();oSavedFile.seek(beginIndex);byte[] b = new byte[1024];int nRead;while((nRead = in.read(b,0,1024)) != -1 ){oSavedFile.write(b,0,nRead);System.out.println(nRead);System.out.println(Thread.currentThread().getName());} }
Multi-thread download
Int length = httpconn. getcontentlength (); randomaccessfile osavedfile = new randomaccessfile ("down.html", "RW"); // osavedfile. setlength (length); // Number of threads 5 Int threadnum = 2; int singlelength = length/threadnum; For (INT I = 0; I