Android cainiao road-multi-thread download

Source: Internet
Author: User

Android cainiao road-multi-thread download
To download the Android client, create an empty file (blank file) on the Android client. The size of the file is the same as that of the server. Start several threads to download the corresponding resources on the server. If all the threads have been downloaded, the server resources have been downloaded. Android client download Logic

Obtain the size of the server resource file (Connection. getContentLength ())

String path = "http: // localhost: 8080/ff.exe"; URL url = new URL (path); // obtain the link HttpURLConnection connection = (HttpURLConnection) url. openConnection (); // parameter connection. setRequestMethod ("GET"); connection. setConnectTimeout (5000); // response code int responseCode = connection. getResponseCode (); if (responseCode = 200) {// get the file size returned by the server long contentLength = connection. getContentLength (); System. out. println ("Server File Size:" + contentLength);} // disconnect the connection. disconnect ();

Create a local blank file of the same size as the server

File file = new File("temp.ext");RandomAccessFile raf= new RandomAccessFile(file, "rw");raf.setLength(contentLength);

Based on the number of threads enabled, server resources are divided into several equal parts (threadCount: Number of threads)

Assume that the file size is 10 bytes. (Length: length) assume three threads (number of threads: threadCount ). Int blockSize = length/threadCount; thread 1 downloads 3 bytes 1-3 0 * blockSize + 1 ~ 1 * blockSize thread 2 download 3 byte 4-6 1 * blockSize + 1 ~ 2 * blockSize thread 3 download 4 byte 7-10 2 * blockSize + 1 ~ 3 * blockSize (length) // start several subthreads to download the corresponding resource long threadCount = 3; long blockSize = contentLength/threadCount; for (int I = 1; I <= threadCount; I ++) {// calculates the start and end position of the download. long startIndex = (I-1) * blockSize + 0; // The server also starts from 0 long endIndex = I * blockSize-1; // The last thread if (I = threadCount) {endIndex = contentLength-1;} System. out. println ("enable thread:" + I + ", download location: (" + startIndex + "~ "+ EndIndex +") "); new DownloadThread (I, startIndex, endIndex, path). start ();}

Create Thread class

Static class DownloadThread extends Thread {private int threadId; private long startIndex; private long endIndex; private String path; public DownloadThread (int threadId, long startIndex, long endIndex, String path) {super (); this. threadId = threadId; this. startIndex = startIndex; this. endIndex = endIndex; this. path = path;} public void run () {URL url; try {url = new URL (path); // obtain the link HttpURL Connection connection = (HttpURLConnection) url. openConnection (); // parameter connection. setRequestMethod ("GET"); connection. setConnectTimeout (5000); // notify the server of the downloaded parameter connection. setRequestProperty ("Range", "bytes =" + startIndex + "-" + endIndex); // response code int responseCode = connection. getResponseCode (); System. out. println ("Server Status Code:" + responseCode); // download the Server File and return 206 if (responseCode = 206) {InputStream I NputStream = connection. getInputStream (); File file = new File ("temp.exe"); RandomAccessFile raf = new RandomAccessFile (file, "rw"); // specify the location where the File is written. seek (startIndex); System. out. println ("Number" + threadId + "thread, start and end position of the file to be written (" + (startIndex) + "," + (endIndex) + ")"); int len = 0; byte [] buf = new byte [1024]; while (len = inputStream. read (buf ))! =-1) {raf. write (buf, 0, len);} inputStream. close (); raf. close (); System. out. println ("no." + threadId + "Threads downloaded") ;}} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}

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.