Android网络连接---多线程下载

已经写了一篇单线程下载,现在再使用多线程下载,下载速度比单线程快得多,相当于是一个人干活和多个人一起干一样。怎么实现多线程下载?
首先看一下结构示意图:
这里写图片描述
从图上就可以看出,多线程是不同的地方开始下载,同时从不同的位置写入本地文件然后拼成完整的文件。
新建一个线程类:

public class MultiThread extends Thread {
public MultiThread(long start, long end, String url, String filePath) {
this.start = start;
this.end = end;
this.urlPath = url;
this.filePath = filePath;
    }
private int sum=0;
private long start;
private long end;
private String urlPath;
private String filePath;

public int getSum() {
return sum;
    }

@Override
public void run() {
try {
            URL url=new URL(urlPath);
            URLConnection connection=url.openConnection();
            connection.setAllowUserInteraction(true);
            connection.setRequestProperty("Range", "bytes=" + start + "-"
+ end);//这个方法切割输入流
InputStream is= connection.getInputStream();//得到输入流
byte [] array=new byte[1024];
            is.read(array);
            File file=new File(filePath);
            RandomAccessFile randomAccessFileile=new RandomAccessFile(file,"rw");//rw代表读写
//这个类用于下载的文件拼接
randomAccessFileile.seek(start);//跳转到开始位置
int index=is.read(array);
while (index!=-1){
                randomAccessFileile.write(array,0,index);//写入
sum+=index;//sum记录长度
                index=is.read(array);
            }
            randomAccessFileile.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在点击事件中新建一个线程,用handler传message更新progressbar

//Handler代码:
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0x23:
progressBar.setProgress(msg.arg1*100/length);
break;
        }
    }
};
//监听点击事件
case R.id.button_more:
new Thread(new Runnable() {
@Override
public void run() {
try {
                String urlPath="http://192.168.0.30:8080/MyWebTest/music/aa.mp3";//自己建的服务器,可以填个网上下载网址
                URL  url = new URL(urlPath);
                URLConnection connection=url.openConnection();
length=connection.getContentLength();//建立连接,得到文件长
File file=new File(Environment.getExternalStorageDirectory(),"cc.mp3");
if(!file.exists()){
                    file.createNewFile();
                }//新建本地文件cc.mp3
MultiThread[] threads=new MultiThread[5];//5个线程下载文件
for (int i=0;i<5;i++){
                    MultiThread thread=null;
if(i==4){//不是整除,最后一个需要下载到结尾
thread = new MultiThread(length / 5*4, length , urlPath, file.getAbsolutePath());
                    }else {
                        thread = new MultiThread(length / 5 * i, length / 5 * (i + 1)-1, urlPath, file.getAbsolutePath());//经尝试这样写长度正好
                    }
                    thread.start();
                    threads[i]=thread;
                }
boolean isFinish=true;//如果长度+10大于文件长了就变成false,不再更新progressbar

while(isFinish){
int sum=0;
for (MultiThread thread:threads){
                        sum+= thread.getSum();//MultiTread传过来的sum,用来记录已下载的文件长
}
                    Message msg=  handler.obtainMessage();//handler用法
msg.what=0x23;
                    msg.arg1=sum;
handler.sendMessage(msg);
if(sum+10>=length){
                        isFinish=false;
                    }
                    Thread.sleep(1000);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }).start();

break;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值