迅雷java_Java 仿迅雷多线程下载

packagenet.webjoy.jackluo.android_json;/*** 1.http Range "bytes="+ start+end

* 2.RandomAccessFile设置写入的位置

* 3开启多线程下载

*

* Created by jackluo on 9/30/15.*/

importandroid.os.Environment;importandroid.os.Handler;importandroid.os.Message;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importjava.io.RandomAccessFile;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;importjava.util.concurrent.Executor;importjava.util.concurrent.Executors;/*** 多线程下载*/

public classDownLoad {//创建一个线程池 创建三个线程

private Executor threadPool = Executors.newFixedThreadPool(3);privateHandler handler;publicDownLoad(Handler handler){this.handler =handler;

}//创建一个线程池对象

static class DownLoadRunnable implementsRunnable{privateString url;privateString fileName;private long start ;//开始位置

private long end;//结束位置

privateHandler handler;public DownLoadRunnable(String url,String fileName,long start,longend,Handler handler){this.url =url;this.fileName =fileName;this.start =start;this.end =end;this.handler =handler;

}

@Overridepublic voidrun() {try{

URL httpUrl= newURL(url);

HttpURLConnection connection=(HttpURLConnection) httpUrl.openConnection();

connection.setRequestMethod("GET");

connection.setReadTimeout(5000);//设置下载位置 向服务器拿到指定的流信息

connection.setRequestProperty("Range", "bytes=" + start + "-" +end);//创建一个文件 根据指定位置写入信息

RandomAccessFile accessFile = new RandomAccessFile(new File(fileName),"rwx");

accessFile.seek(start);//设置读写的位置

InputStream inputStream =connection.getInputStream();byte[] b = new byte[1024*4];//设置缓冲区的大小存

int len=0;while ((len = inputStream.read(b))!=-1){

accessFile.write(b,0,len);

}if (accessFile !=null){

accessFile.close();

}if (inputStream !=null){

inputStream.close();

}//给主线程发送一个消息

Message message = newMessage();

message.what= 1;

handler.sendMessage(message);

}catch(MalformedURLException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}

}public voiddownLoadFile(String url){try{

URL httpUrl= newURL(url);

HttpURLConnection connection=(HttpURLConnection) httpUrl.openConnection();

connection.setRequestMethod("GET");

connection.setReadTimeout(5000);int count = connection.getContentLength();//获取了图片的大小

int block = count/3;//算线程的启始位置

String fileName =getFileName(url);

File parent=Environment.getExternalStorageDirectory();

File fileDownLoad= newFile(parent,fileName);/*** 11 / 3 = 每个下载 3个字节3 还余出2个字节空间

* 第一个线程 0-2

* 第二个线程 3-5

* 第三个线程 6-10

*

**/

for (int i=0;i<3;i++){long start = i*block;long end = (i+1)*block -1;if (i==2){

end=count;

}

DownLoadRunnable runnable= newDownLoadRunnable(url, fileDownLoad.getAbsolutePath(),start,end,handler);//提交任务

threadPool.execute(runnable);

}

}catch(MalformedURLException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}/*** 获取 URL后缀名*/

publicString getFileName(String url){return url.substring(url.lastIndexOf("/")+1);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值