Java multi-thread download implementation example

Source: Internet
Author: User

Import Java. io. file; import Java. io. filenotfoundexception; import Java. io. inputstream; import Java. io. randomaccessfile; import java.net. httpurlconnection; import java.net. URL; public class main {/*** @ Param ARGs * @ throws exception */public static void main (string [] ARGs) throws exception {New Main (). download ("http://gdown.baidu.com/data/wisegame/a3d96bf8e8a5102c/baiduliulanqi_25.apk", 5);} public void d Ownload (string downpath, int threadnum) throws exception {URL url = new URL (downpath); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("get"); // inputstream in = Conn. getinputstream (); If (Conn. getresponsecode () = 200) {int size = Conn. getcontentlength (); file = new file (downpath. substring (downpath. lastindexof ("/") + 1); // "RWD" will immediately Synchronize data to the device to prevent loss of mobile phone data without power randomaccessfile RAF = new randomaccessfile (file, "RWD"); RAF. setlength (size); // There is no data Raf. close (); int block = size % threadnum = 0? Size/threadnum: Size/threadnum + 1; for (INT I = 0; I <threadnum; I ++) {New downthread (I, block, URL, file ). start () ;}} else {system. out. println ("Download failed") ;}/// download Thread class private class downthread extends thread {private int ID; private int block; // download the Private URL of each thread; // address private file; // download the public downthread (int I, int block, URL, file) that stores the object {This. id = I; this. block = block; this. url = URL; this. fi Le = file;} public void run () {int start = ID * block; int end = (ID + 1) * block-1; try {randomaccessfile RAF = new randomaccessfile (file, "RWD"); RAF. seek (start); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("get"); // specify the Object Location bytes = "1-1000" conn through the header field. setrequestproperty ("range", "bytes =" + start + "-" + end); // The return code is 206if (Co Nn. getresponsecode () = 206) {byte [] buffer = new byte [1024]; inputstream in = Conn. getinputstream (); int Len = 0; while (LEN = in. read (buffer ))! =-1) {Raf. write (buffer, 0, Len);} Raf. close (); In. close (); system. out. println ("thread:" + ID + "download completed");} else {system. out. println ("failed") ;}} catch (exception e) {e. printstacktrace ();} system. out. println ("");}}}

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.