Multi-threaded downloads increase speed by occupying server resources.
Each thread should download a different location of a file, if there is now a resource of length 10, there are three threads, then the first thread downloads the length is 012, the second is 345, the fourth is 6789
So the question is, what is the length of each thread's download?
The thread's ID is 0,1,2
Number of downloads per thread = total length/number of threads
Size=10/3
Start:id*size
End: (id+1) *size-1
The end position of the last thread is: total Length-1
PackageDown ;ImportJava.io.File;ImportJava.io.InputStream;ImportJava.io.RandomAccessFile;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classDown {Static intThreadCount = 3; StaticString Path = "Http://192.168.21.1:8080/ok/ideaIU-14.1.3.exe"; /** * @paramargs*/ Public Static voidMain (string[] args) {Try{URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setreadtimeout (5000); Conn.setconnecttimeout (5000); if(Conn.getresponsecode () ==200){ //the length of the first request to download intLength =conn.getcontentlength (); //The file name should be truncated from the stringFile File =NewFile ("Ideaiu-14.1.3.exe"); //generate temporary files before downloading, occupying a locationRandomaccessfile RDM =NewRandomaccessfile (file, "RWD"); //to the temporary file sizerdm.setlength (length); //CloseRdm.close (); //calculate the length that each thread should download intSize = length/ThreadCount; //assign start and end locations for each thread for(inti = 0; I <threadCount; i++) { intStart = i*size; intEnd = (i+1) *size-1; //the end position of the last thread is the total length if(i==threadcount-1) {End=length; } System.out.println (I+ "Thread download Interval" +start+ "---" +end); //each time a new thread is called NewDownthread (Start, end, I). Start (); } } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}//ThreadsclassDownthreadextendsthread{Private intstart; Private intend; Private intthreadId; PublicDownthread (intStartintEndintthreadId) { This. Start =start; This. end =end; This. ThreadId =threadId; } @Override Public voidrun () {Try { //Request Network againURL url =NewURL (Down.path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setreadtimeout (5000); Conn.setconnecttimeout (5000); //set the length of the file for this requestConn.setrequestproperty ("Range", "bytes=" +start+ "-" +end); //request part of the data is 206 if(Conn.getresponsecode () ==206){ //the file input stream that gets the requested lengthInputStream is =Conn.getinputstream (); byte[] B =New byte[1024]; intLen = 0; //record the number of bytes downloaded each time intTotle =0; //The file name should be truncated from the stringFile File =NewFile ("Ideaiu-14.1.3.exe"); //the file that occupies the previous position, the equivalent of the output streamRandomaccessfile RDM =NewRandomaccessfile (file, "RWD"); //change the start position of the write fileRdm.seek (start); while((Len=is.read (b))!=-1){ //every time the data in the stream is written to the temp file .Rdm.write (b, 0, Len); Totle+=Len; System.out.println (ThreadId+ "Threads" + "downloaded" +totle); } rdm.close (); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Java Multi-Threaded download