public class Mainactivity extends Activity {
protected static final int down = 0;
protected static final int SEER = 1;
Private context context;
Private EditText Ed;
Private ProgressBar PB;
public static int threadcount=3;
public static int runnling=3;
public int cureatprocess=0;//Current progress
Private Handler Handler =new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case-down:
Toast.maketext (Getapplicationcontext (), "Download Error", 0). Show ();
Break
Case SEER:
Toast.maketext (Getapplicationcontext (), "Download Failed", 0). Show ();
Break
}
};
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Ed= (EditText) Findviewbyid (R.id.ed_path);
pb= (ProgressBar) Findviewbyid (R.ID.PB);
}
public void paly (view view) {
Final String Path=ed.gettext (). toString (). Trim ();
if (textutils.isempty (path)) {
Toast.maketext (Getapplicationcontext (), "Please enter Address", 0). Show ();
Return
}
New Thread () {
public void Run () {
String path= "Http://down9.869v.com/setup_445.exe";
try {
URL url=new url (path);
HttpURLConnection con= (httpurlconnection) url.openconnection ();
Con.setreadtimeout (6000);
Con.setrequestmethod ("GET");
int code= Con.getresponsecode ();
if (code==200) {
int length= con.getcontentlength ();//The requested location has been set//returns the input stream of the current position corresponding file
Pb.setmax (length);//Get progress bar maximum
Randomaccessfile raf=new Randomaccessfile ((/"Sdscard"/+ "421.exe", "RWD");//record the length of data downloaded by the current thread
Raf.close ();
System.out.print ("File length:" +length);
int Bolak =length/threadcount;
for (int threadid=1;threadid<=threadcount;threadid++) {
int starindex= (threadID-1) *bolak;
int endindex=threadid*bolak-1;
if (Threadid==threadcount) {
Endindex=length;
}
SYSTEM.OUT.PRINTLN ("Thread" +threadid+ "Download---->" +starindex+ "----->" +endindex ");
New MyThread (Path, ThreadID, Starindex, EndIndex). Start ();
}
}else{
System.out.print ("read length failed");
Message msg=new message ();
Handler.sendmessage (msg);
Msg.what=seer;
}
} catch (Exception e) {
E.printstacktrace ();
Message msg=new message ();
Handler.sendmessage (msg);
Msg.what=down;
}
}
}.start ();
}
public class MyThread extends thread{
Public MyThread (String path, int threadID, int starindex, int endIndex) {
Super ();
This.path = path;
This.threadid = ThreadID;
This.starindex = Starindex;
This.endindex = EndIndex;
}
Private String path;
private int ThreadID;
private int starindex;
private int endIndex;
public void Run () {
try {
Check if the file is in the record download length if there is data reading this file
File tempfile =new file (context. mode_private+threadid+ ". txt");
if (Tempfile.exists () &&tempfile.length () >0) {
FileInputStream fis=new FileInputStream (tempfile);
Byte[] Tpem=new byte[1024];
int Leng =fis.read (TPEM);
String Dowmlod =new string (Tpem,0,leng);
int downlodalenint= integer.parseint (dowmlod);
int Alreadydown =downlodalenint-starindex;
cureatprocess+=alreadydown;//calculate the value of the last breakpoint that was downloaded
starindex=downlodalenint;//Modify the actual start location of the download
Fis.close ();
}
URL url=new url (path);
HttpURLConnection con = (httpurlconnection) url.openconnection ();
Con.setrequestmethod ("GET");
Con.setrequestproperty ("Range", "byte=" +starindex+ "-" +endindex);
SYSTEM.OUT.PRINTLN ("Thread Real download location" +threadid+ "Download----" +starindex);
Con.setreadtimeout (8000);
int Code=con.getresponsecode ();
SYSTEM.OUT.PRINTLN ("code" +code);
InputStream is= Con.getinputstream ();
Randomaccessfile raf=new Randomaccessfile ((/"Sdscard"/+ "421.exe", "RWD");
Raf.seek (Starindex);
int len=0;
Byte[]buffer=new byte[1024];
int total=0;
while ((Len=is.read (buffer))!=-1) {
Randomaccessfile Flie =new Randomaccessfile ((/"Sdscard"/+threadid+ ". txt", "RWD");
Raf.write (Buffer,0,len);
Total+=len;
SYSTEM.OUT.PRINTLN ("Thread" +threadid+ "total:" +total);
Flie.write ((total+starindex+ ""). GetBytes ());//Record the location of the download
Flie.close ();
Synchronized (mainactivity.this) {
cureatprocess+=len;//record Current Total progress
Pb.setprogress (cureatprocess);
}
}
Is.close ();
Raf.close ();
SYSTEM.OUT.PRINTLN ("Thread id" +threadid+ "Download Complete ...");
} catch (Exception e) {
E.printstacktrace ();
}finally{
Threadfins ();
}
}
Private synchronized void Threadfins () {
runnling--;
if (runnling==0) {
for (int i=1; i<=3;i++) {
File File=new file (/"Sdscard"/+i+ ". txt");
File.delete ();
}
System.out.println ("File download ban bi delete all download Records");
}
}
}
}
Multithreaded Java code porting to Android