package liuke.multidownload;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final int THREAD_COUNT=3;
int currentProgress=0;
public static String path="http://10.0.2.2/tom.exe";
static int finishedId=1;
ProgressBar pb;
Handler handler;
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb=(ProgressBar) findViewById(R.id.proBar);
setListener();
tv=(TextView) findViewById(R.id.tv);
createHandler();
}
private void createHandler() {
// TODO Auto-generated method stub
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
tv.setText(pb.getProgress()*100/pb.getMax()+"%");
}
};
}
private void setListener() {
// TODO Auto-generated method stub
findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run() {
try {
URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode()==200){
int length = conn.getContentLength();
pb.setMax(length);
File file=new File(Environment.getExternalStorageDirectory(),"tom.exe");
RandomAccessFile raf=new RandomAccessFile(file, "rwd");
raf.setLength(length);
raf.close();
int size=length/THREAD_COUNT;
for(int i=0;i<THREAD_COUNT;i++){
int startIndex=i*size;
int endIndex=(i+1)*size-1;
if(i==THREAD_COUNT-1){
endIndex=length-1;
}
new DownThread(startIndex, endIndex, i).start();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
});
}
class DownThread extends Thread{
int startIndex;
int endIndex;
int id;
public DownThread(int startIndex, int endIndex, int id) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
this.id = id;
}
@Override
public void run() {
try {
File progressfile=new File(Environment.getExternalStorageDirectory(),id+".txt");
if(progressfile.exists()){
FileInputStream fis=new FileInputStream(progressfile);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
int lastProgress=Integer.parseInt(br.readLine());
startIndex += lastProgress;
currentProgress+=lastProgress;
pb.setProgress(currentProgress);
fis.close();
}
URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("Range","bytes="+startIndex+"-"+endIndex);
System.out.println(conn.getResponseCode());
if(conn.getResponseCode()==206){
InputStream is = conn.getInputStream();
File file=new File(Environment.getExternalStorageDirectory(),"tom.exe");
RandomAccessFile raf=new RandomAccessFile(file, "rwd");
byte[]buffer=new byte[1024];
int len=0;
int total=0;
raf.seek(startIndex);
while(-1!=(len=is.read(buffer))){
raf.write(buffer, 0, len);
total=startIndex+len;
currentProgress+=len;
pb.setProgress(currentProgress);
Message msg=Message.obtain();
msg.arg1=currentProgress;
handler.sendMessage(msg);
System.out.println("线程"+id+len);
RandomAccessFile progressRaf=new RandomAccessFile(progressfile,"rwd");
progressRaf.write((total+"").getBytes());
progressRaf.close();
}
raf.close();
finishedId++;
synchronized (path) {
if(finishedId==THREAD_COUNT){
for(int i=0;i<THREAD_COUNT;i++){
File f=new File(Environment.getExternalStorageDirectory(),i+".txt");
f.delete();
}
finishedId=0;
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
多线程下载和断点续传
最新推荐文章于 2022-12-27 17:10:26 发布