首先,先导依赖
compile 'com.loopj.android:android-async-http:1.4.9'
加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.bwie.test.thread.MainActivity">
<TextView
android:id="@+id/tv_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载地址:"
android:textSize="25dp"/>
<Button
android:onClick="download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:textSize="25dp"/>
</LinearLayout>
//主要代码
public class MainActivity extends AppCompatActivity {
protected static final String TAG = "MainActivity";
//下载线程的数量
private final static int threadsize = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void download(View v){
new Thread(){
@Override
public void run() {
super.run();
HttpClient client=new DefaultHttpClient();
String uri = "http://c.hiphotos.baidu.com/image/pic/item/b90e7bec54e736d1e51217c292504fc2d46269f3.jpg";
HttpHead request=new HttpHead(uri);
try {
HttpResponse response = client.execute(request);
if(response.getStatusLine().getStatusCode()==200){
Header[] headers = response.getHeaders("Content-Length");
String value = headers[0].getValue();
int filelength = Integer.parseInt(value);
String name = getFileName(uri);
File file = new File(Environment.getExternalStorageDirectory(),name);
//随机访问文件
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//设置文件的大小
raf.setLength(filelength);
//关闭
raf.close();
//计算每条线程的下载量
int block = (filelength%threadsize == 0)?(filelength/threadsize):(filelength/threadsize+1);
//开启三条线程执行下载
for(int threadid=0;threadid<threadsize;threadid++){
new DownloadThread(threadid, uri, file, block).start();
}
}
} catch (IOException e) {
}
}
private String getFileName(String uri) {
return uri.substring(uri.lastIndexOf("/")+1);
}
}.start();
}
//线程下载类
private class DownloadThread extends Thread{
private int threadid;//线程的id
private String uri;//下载的地址
private File file;//下载文件
private int block;//下载的块
private int start;
private int end;
public DownloadThread(int threadid, String uri, File file, int block) {
super();
this.threadid = threadid;
this.uri = uri;
this.file = file;
this.block = block;
//计算下载的开始位置和结束位置
start = threadid * block;
end = (threadid + 1)*block -1;
}
//下载 状态码:200是普通的下载 206是分段下载 Range:范围
@Override
public void run() {
super.run();
try {
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//跳转到起始位置
raf.seek(start);
//分段下载真实内容
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(uri);
request.addHeader("Range", "bytes:"+start+"-"+end);//添加请求头
HttpResponse response = client.execute(request);
if(response.getStatusLine().getStatusCode() == 200){
InputStream inputStream = response.getEntity().getContent();
//把流写入到文件
byte[] buffer = new byte[1024];
int len = 0;
while((len = inputStream.read(buffer)) != -1){
//写数据
raf.write(buffer, 0, len);
}
inputStream.close();
raf.close();
Log.i(TAG, "第"+threadid+"条线程下载完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}