Multi-threaded download file, and breakpoint download

Source: Internet
Author: User

A: Preface

Multi-threaded download files, there may be some students have not heard too much, but the breakpoint download is certainly heard, that is to say, even if you restart the computer, the Thunder after the restart will also go to the original place to download, then this is how to do it?

Two: code example

Give the code directly,

2.1. Classic Code

The two lines of classic code are:

// set the start and end location of the download conn.setrequestproperty ("Range", "bytes=" +start+ "-" +end+ "");
// set the start location for read and write New Randomaccessfile ("E:/test/temp.exe", "RW"); Raf.seek (start);

2.2. Complete code:
Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.RandomAccessFile;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.util.Date; Public classDemo {/*** Number of download threads <br> * I used 5 threads to download for 37 seconds, 1 threads to download for 45 seconds*/    Private Static intnum = 5;  Public Static voidMain (string[] args) {threaddown (); }     Public Final Static voidThreaddown () {Try{URL URL=NewURL ("Http://dldir1.qq.com/qqfile/qq/QQ8.2/17724/QQ8.2.exe"); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); intSize =conn.getcontentlength (); //each purebred download that size            intblock = size/num; //This class supports random access to a file, the first parameter is the file name, the second is the access mode, and RW represents the read-writeRandomaccessfile RAF =NewRandomaccessfile ("E:/test/temp.exe", "RW"); //create an empty file locally with the same size as the server's fileraf.setlength (size);            Raf.close (); //define a location to start the download, end the download location            intstart; intend;  for(inti = 0; i < num; i++) {Start= i *Block; if(num = i + 1) {End=size; } Else{End= Block * (i + 1)); } System.out.println ("Start:" + start + ", End:" +end); Threaddown Down=NewThreaddown (start,end);            Down.start (); }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}classThreaddownextendsthread{Private intstart; Private intend;  PublicThreaddown (intStartintend) {         This. Start =start;  This. end =end; }     Public voidrun () {LongStartTime =NewDate (). GetTime (); String name=Thread.CurrentThread (). GetName (); Try{System.out.println (name+ ": Start"); URL URL=NewURL ("Http://dldir1.qq.com/qqfile/qq/QQ8.2/17724/QQ8.2.exe"); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); //Here is also the focus, set the start byte of the request and the end byteConn.setrequestproperty ("Range", "bytes=" +start+ "-" +end+ ""); InputStream in=Conn.getinputstream (); Randomaccessfile RAF=NewRandomaccessfile ("E:/test/temp.exe", "RW"); //set the starting point for reading and writing, here is the keyRaf.seek (start); byte[] buf =New byte[1024]; intLen = 0;  while((Len=in.read (BUF))! =-1) {raf.write (buf,0, Len);            } in.close ();            Raf.close (); LongEndTime =NewDate (). GetTime (); LongUsetime = (endtime-starttime)/1000; SYSTEM.OUT.PRINTLN (Name+ "Download completed, Total time:" + usetime + "seconds"); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }    }

Three: Breakpoint download

Want to see the above code, breakpoint download the principle of everyone must also know, the same is also used

Set the start and end location of the download Conn.setrequestproperty ("Range", "bytes=" +start+ "-" +end+ "");
Set the starting position for read and write Randomaccessfile RAF = new Randomaccessfile ("E:/test/temp.exe", "RW"); Raf.seek (start) ;

Multi-threaded download file, and breakpoint download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.