The original text was downloaded in 52.
It feels like we're not familiar with IO and URLs. It's very simple. I've tried it before and it's complicated, but it's not good to say. Actually most want is similar to IDM this multithreading, hehe.
Code:
Package Com.test_four;import java.io.ioexception;import java.io.inputstream;import java.io.RandomAccessFile; Import Java.net.httpurlconnection;import Java.net.URL; Public classDemo { Public Static intThreadCount =3; Public Static voidMain (string[] args) throws Exception {//1. Connect to the server, get a file, get a file, get the length of the file, create a local size and the server file as large as the temporary fileString Path ="Https://admdownload.adobe.com/bin/livebeta/flashplayer24_va_install.exe"; URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout ( the); Conn.setrequestmethod ("GET"); intCode =Conn.getresponsecode (); if(Code = = $) { //The length of the data returned by the server is actually the length of the file intLength =conn.getcontentlength (); System. out. println ("Total file Length:"+length);//Create a temporary file of the same size as the server-side file locally on the clientRandomaccessfile RAF =NewRandomaccessfile ("Xxx.exe","RW");//Specifies the length of the file createdraf.setlength (length); Raf.close ();//Let's say three threads to download a resource//average size of files downloaded per thread intBlockSize = length/ThreadCount; for(intThreadId =1; threadid<=threadcount;threadid++){//first thread download start location intStartIndex = (ThreadId-1)*blockSize; intEndIndex = Threadid*blocksize-1; if(threadId = = threadcount) {//the last thread to download is a little bit longer .EndIndex =length; } System. out. println ("Threads:"+ ThreadId +"Download:---"+ StartIndex +"--->"+EndIndex); Newdownloadthread (Path, threadId, StartIndex, EndIndex). Start (); } } Else{System. out. println ("Server Error"); } } Public Static classDownloadthread extends thread{Private intthreadId; Private intStartIndex; Private intEndIndex; PrivateString Path; @Override Public voidrun () {Try{URL URL=NewURL (path); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setconnecttimeout ( the); Connection.setrequestmethod ("GET");//IMPORTANT: Request the server Download section of the file to specify the location of the fileConnection.setrequestproperty ("Range","bytes="+ StartIndex +"-"+EndIndex); intCode = Connection.getresponsecode ();//request all resources from the server OK if you request some resources from the server 206 OK//System.out.println ("code:" + code);InputStream is=connection.getinputstream ();//The requested location has been set and the input stream for the file corresponding to the current location is returnedRandomaccessfile RAF =NewRandomaccessfile ("Xxx.exe","RW"); Raf.seek (startIndex);//where to start writing when writing files randomly intLen =0; byte[] buffer =New byte[1024x768]; while(len = is. Read (buffer))!=-1) {raf.write (buffer,0, Len); } is. Close (); Raf.close (); System. out. println ("Threads:"+threadid +"Download Complete"); } Catch(IOException e) {e.printstacktrace (); } } /** threadId thread ID * startIndex thread download start location * EndIndex thread download End location * Path to download file on server */ PublicDownloadthread (String Path,intThreadId,intStartIndex,intEndIndex) {super (); This. ThreadId =threadId; This. StartIndex =StartIndex; This. EndIndex =EndIndex; This. Path =path; } }}
Multi-threaded Download (GO)