Principle of multi-thread download in Android

Source: Internet
Author: User

Principle of multi-thread download in Android

The formula for calculating the start and end position of each download thread is as follows:

File read/write mode 4 medium type

Project source code directory

Package cn. itcast. download; import java. io. file; import java. io. inputStream; import java. io. randomAccessFile; import java.net. httpURLConnection; import java.net. URL; public class MulThreadDownloader {public static void main (String [] args) throws Exception {String path = "http: // 219.245.72.241: 8080/web3/gaosu. jsp "; // the address of the Server File int threadsize = 3; // enable three threads. The number of threads enabled in the android application cannot be too many. new MulThreadDownloader (). down Load (path, threadsize);} private void download (String path, int threadsize) throws Exception {URL downpath = new URL (path); HttpURLConnection conn = (HttpURLConnection) downpath. openConnection (); conn. setConnectTimeout (5000); conn. setRequestMethod ("GET"); if (conn. getResponseCode () = 200) {int length = conn. getContentLength (); // get the length of the network File file = new File (getFileName (path); RandomAccessFile AccessFile = new RandomAccessFile (file, "rwd"); // generate the local file accessFile. setLength (length); accessFile. close (); // calculate the volume of data downloaded by each thread. int block = length % threadsize = 0? Length/threadsize: length/threadsize + 1; for (int threadid = 0; threadid <threadsize; threadid ++) {new DownloadThread (threadid, downpath, block, file ). start () ;}}// downloads private final class DownloadThread extends Thread {private int threadid; private URL downpath; private int block; private File file; public DownloadThread (int threadid, URL downpath, int block, File file) {this. thread Id = threadid; this. downpath = downpath; this. block = block; this. file = file;} public void run () {int startposition = threadid * block; // Where to download data from the network file int endposition = (threadid + 1) * block-1; // the position at which the network file is downloaded. // It indicates that the thread starts to download the file from the startposition of the network file, and the download ends at the endposition. // Range: bytes = startposition-endposition try {RandomAccessFile accessFile = new RandomAccessFile (file, "rwd"); accessFile. seek (star Tposition); // move the pointer to a certain position in the file. HttpURLConnection conn = (HttpURLConnection) downpath. openConnection (); conn. setConnectTimeout (5000); conn. setRequestMethod ("GET"); conn. setRequestProperty ("Range", "bytes =" + startposition + "-" + endposition); // The region download request code is not 200, but 206 // if (conn. getResponseCode () = 206) {InputStream inStream = conn. getInputStream (); byte [] buffer = new byte [1024]; int len = 0; while (le N = inStream. read (buffer ))! =-1) {accessFile. write (buffer, 0, len);} accessFile. close (); inStream. close (); System. out. println ("no." + (threadid + 1) + "thread download completed");} catch (Exception e) {e. printStackTrace () ;}}/ *** get file name * @ param path download path * @ return */private static String getFileName (String path) {return path. substring (path. lastIndexOf ("/") + 1 );}}

The client needs to download the gaosu. jsp file in the web3 directory of the server.

Start tomcat and suspend server web3

Run the preceding java program and the result is displayed.

In this case, the source code directory of the project generates the gaZ license that has been downloaded? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3uannwzss8/jxiciAvPg0KPGltZyBhbHQ9" here write picture description "src =" http://www.bkjia.com/uploads/allimg/150617/041S92N3-7.png "title =" "/>

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.