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 ("");}}}