Android for multi-threaded download:
First look at the following:
UI interface
Multi-threaded download when log print interface
To get started, we first connect the files that need to be downloaded through the HttpURLConnection class:
New Thread (New Runnable () {@Overridepublic void run () {try {url = new URL (downurl); HttpURLConnection conn = (httpurlconnection) url.openconnection ();//Conn.connect (); totallength = Conn.getcontentlength (); System.out.println (totallength); singlelength = (totallength + THREADNUM-1)/Threadnum;startdown ();} catch (Exception e) {e.printstacktrace ();}}}). Start ();
Which calculates the total byte length of the downloaded file according to the Conn.getcontentlength () function
Get the total length after passing singlelength = (totallength + THREADNUM-1)/threadnum; Calculate the module unit length per thread, and here you can think about why this formula is not directly
Singlelength = totallength/threadnum; calculation?
It is convenient to calculate the length of a single thread in charge of the module, we call multiple threads directly through the loop, each thread is responsible for its own download module, before we need to calculate the start and end position of each block download
private void Startdown () {for (int i = 0; i < threadnum; i++) {File Cachefile = new File (cachedir, "temp" + i); Long beg in = i * singlelength + cachefile.length (); Long end = begin + SingleLength-1; System.out.println ("First" + i + "cache files:" + Begin + "-" + end); New Downthread (Begin, end, Cachefile). Start ();}}
Then turn on the multi-threaded download task
Class Downthread extends Thread{private long begin;private long end;private File cachefile;public downthread (long begin, L Ong end, File cachefile) {super (); this.begin = Begin;this.end = End;this.cachefile = Cachefile;} @Overridepublic void Run () {try {httpurlconnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestproperty ("Range", "bytes=" + Begin + "-" + end); FileOutputStream out = new FileOutputStream (cachefile), inputstream in = Conn.getinputstream (); byte[] buffer = new BYTE[10 24];int len = 0;while ((len = in.read (buffer))! =-1) out.write (buffer, 0, Len); In.close (); Out.close (); System.out.println (cachefile.tostring () + "cache file download Complete"); Megagefile ();} catch (IOException e) {e.printstacktrace ();}}}
Here the core code is both throughconn.setrequestproperty ("Range", "bytes=" + Begin + "-"+ END); This statement controls the file location downloaded by each request server, and the other places are exactly the same as the ordinary request;
After each thread is completed, we need to merge the cache files for each thread, and when merging the cache files, it is necessary to determine that each thread-responsible module is downloaded and guaranteed to be merged sequentially.
Here's how it's implemented, namely Megagefile (); What is the core code of this function?
I was just starting out. download file content 1234567890 after merging, it becomes 5678123490; There's no code left for you to think about.
Source download (with cache file Merge method): Click to open the link
Android multi-threaded download file