The code is very simple and practical.
package android.mooc.Tools; Import Java.io.BufferedInputStream; Import Java.Io.file; Import Java.randomaCcessFile; Import port java.net.urlConnection; Import android.util.log; Public Class FiledownloadThread Extends Threads {Private Static Final Int buffer_size = 1024; Private file file; Private Int Startposition; PRI Vate int EndPosition; Private int Curposition; // Used to identify whether the current thread is downloaded to complete the Private Boolean Finished = False; Private; int Downloadsize; Private Boolean State; Boolean Destory; Public Boolean Isdestory () {Return Destory;} Public void Setdestory (Boolean Destory) Y = Destory;} Public FiledownloadThread (url url, file file, int Startposition, int EndPosition) {this.url = url; this.file = file; this.startPosition = StartPosition; this.CurPosition = StartPosition; this.endposition = endposition; this s.downloadsize = 0;}} @Override Public Void Run () {Destory = false; state = true; bufferedInputStream Bis = Null; RandomaccessFile Fos = Null; byte [] buf = new byte [buffer_size]; = url.openconnection (); con.setallowuserinteraction (true); // Set At the starting point of the current thread, the end point Con.SetRequestProperty ("Range", "Bytes =" + Startposition + "-" + Endposition); con.setRequestproperty ("accept", "*/*"); UESTPROPERTY ("Connection "," Keep-Alive "); con.setRequestproperty (" Accept-Language "," zh-cn "); con.setRequestProperty (" Charset "," UTF-8 "); User-agent " , "Mozilla/4.0 (composition; msie 8.0; Windows NT 5.2; Trident/4.0; .NET ClR 1.1.4322;" + ".NET ClR 2.0.50727; .NET CLR 3.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) ")"); // Use RandomaCcessFile in java to randomly read and write FOS = New RandomaCcessFile (File, "RW"); // Set the position of starting file Fos.seek (StartPositation). ; bis = new bufferedInpputStream (con.getinputStream ()); // starts to circulate the form of reading and write files. EP (2000) ;} Int len = bis.read (buf, 0, buffer_size); if (len! = -1) {fos.write (buf, 0, len); Curposition = Curposity + Len; if (Curposition> EndPosition> ) {doubleadsize + = Len - (Curposition -endposition);} else {downloadSize + = Len;}} log.i ("333", "run" + "len =" + len); Finished = TRUE; BIS.Close (); Fos.close ();} Catch (Exception E) {e.printstacktrace ();}} Public Boolean Isstate () {Return State;} Public v Oid SetState (Boolean State) {this .State = state;} Public Boolean Isfinished () {Return Finished;} Public int GetdownloadSize () {Return doubledsize;} Public voidLo adsize (int doubleAdsize) {this.downloadsize = downloadSize;}}}The above is all the contents of this article. I hope everyone can like it.