本示例介绍在Android平台下通过HTTP协议实现断点续传下载。1.多线程下载,2.支持断点。使用多线程的好处 :是文档下载会提升文本文件的速度 HttpURLConnection.getContentLength();//获取下载文件的长度 RandomAccessFile file = new RandomAccessFile("QQWubiSetup.exe","rwd"); file.setLength(filesize);//设置本地文件的长度 (2)根据文件长度和线程数计算每条线程下载的数据长度和下载位置。
如:文件的长度为6M,线程数为3,那么,每条线程下载的数据长度为2M,每条线程开始下载的位置如下图所示。例如10M大小,使用3个线程来下载, 线程下载的数据长度 (10%3 == 0 ? 10/3:10/3+1) ,第1,2个线程下载长度是4M,第三个线程下载长度为2M 下载开始位置:线程id*每条线程下载的数据长度 = ? 下载结束位置:(线程id+1)*每条线程下载的数据长度-1=? (3)使用Http的Range头字段指定每条线程从文件的什么位置开始下载,下载到什么位置为止, 如:指定从文件的2M位置开始下载,下载到位置(4M-1byte)为止
代码如下:HttpURLConnection.setRequestProperty("Range", "bytes=2097152-4194303"); (4)保存文件,使用RandomAccessFile类指定每条线程从本地文件的什么位置开始写入数据。 RandomAccessFile threadfile = new RandomAccessFile("QQWubiSetup.exe ","rwd"); threadfile.seek(2097152);//从文件的什么位置开始写入数据注意在读写的权限
1 断续下载提供位置2 给下载 判断sdk 的状态3 分成线程 规划4 正式下载 可以断续进行下载1链接服务器httpurlconnetionget请求传输 yongpos的 方式进行 交接settimeout对sd卡的的判定得到文件大小 的到本地大小空间为每条下城的得到资源文件的大小数据库 大小数据库的单里模式放置错乱 删除 线程 当下爱完成后进行线程的分配多分线程的资源 最后一个线程雄六进行写入rwd 权限 读写怎么传到activity里通过广播 传参 动态广播传对象 动作的设定对每个文件 三条线线程的检查线程的叠加线程中的来回跳转下载文件
实体里 id 名称 url leght size 数据库保存 id lujing 开始 结束 下载进度怎样为每条编程 分配任务多线程断点下载均分 最后那个县城多占写单例模式怎么知道 那三条是哪个个文件的线程通过id 进行判读通过id 传还可以传url 名字和url 时间戳边下边写数据库 的技术(进度时时更新)finish 保存下载的进度 时时保存进度 rwd 时时的缓存 保存进度6 资源文档 写到文件里7 动态的更新进度8 保证进度条来回的条 叠加线程蝶加进度条来回的跳线程没关 有点了一下 相当于开了两次的线程9
提升性能 15——20节省资源 nofifay getview的方法得到网络上的资源 进行解析public name;ProgressBar down_progress;TextView shuzhi;Button down_stop;Button down_start;}6//下载进度的更新 传两个参数一个是 id和进度条的位置public void RefleshProgress(int progress,int id){int b=0;for(int i=0;i<list.size();i++){if(list.get(i).getId()==id){b=i;}}SoftwareClass
software=list.get(b);software.setFinished(progress);notifyDataSetChanged();}7在异步的线程中要做的事 private SoftwareClass soft; //要下载的文件private MySQLService sqlservice; //线程数据库方法private String IP; //IP地址private int mfinished=0; //下载进度public boolean isDown; //下载暂停标签private
ArrayList taskList; //管理下载线程//线程池public static ExecutorService sExecutorService=Executors.newCachedThreadPool();private Context conn;public MyDownTask(SoftwareClass soft,Context conn,String IP){this.soft=soft;this.conn=conn;sqlservice=new MySQLService(conn);this.IP=IP;}/*
* 第三步 为每条下载线程分配下载的区域范围 */public void downLoad(){isDown=false;ArrayList threadList=sqlservice.getList(IP+soft.getUrl());if(threadList.size()==0){int resources=soft.getLength()/3;for(int i=0;i<3;i++){int start=i*resources;int end=(i+1)*resources-1;ThreadClass
threadclass=new ThreadClass(i,IP+soft.getUrl(),start,end,0);if(i==2){threadclass.setEnd(soft.getLength());}//把新添加的下载文件的下载线程信息添加到数据库中sqlservice.insertThreadClass(threadclass);threadList.add(threadclass);}}//管理下载任务的集合taskList=new ArrayList();for(ThreadClass
thread:threadList){Task TT=new Task(thread);MyDownTask.sExecutorService.execute(TT); //用线程池启动线程taskList.add(TT);}}//判断每个文件下载是否完成public synchronized void checkAllThreadFinished(){boolean isAllFinished=true;for(int i=0;i>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>./*
* 第四步 执行下载操作 *///下载操作就是对每条下载线程的下载资源的操作public class Task extends Thread{private ThreadClass threadclass;private boolean isFinished=false; //确认每条线程的下载任务是否完成public Task(ThreadClass threadclass){this.threadclass=threadclass;}//操作步骤public void run(){HttpURLConnection
http=null;RandomAccessFile access=null;InputStream in=null;try {URL url=new URL(threadclass.getUrl());http=(HttpURLConnection) url.openConnection();http.setRequestMethod("GET");http.setReadTimeout(5000);int start=threadclass.getStart()+threadclass.getFinished();
//如果之前下载过就把该线程的开始位置和下载的资源相加http.setRequestProperty("Range", "bytes="+start+"-"+threadclass.getEnd());File file=new File(MyService.DOWN_FILE,soft.getName()); //下载文件的路径access=new RandomAccessFile(file, "rwd"); //为暂停后的下载作标签access.seek(start); mfinished=mfinished+threadclass.getFinished();
//之前若下载过,把之前的起源加起来Intent intent2=new Intent(MyService.UPDATA_ACTION); //用于下载进度更新数据发送int code=http.getResponseCode();if(code==206){in=http.getInputStream();int len=-1;byte[] arr=new byte[1024*4];long time=System.currentTimeMillis(); //当前的系统时间while((len=in.read(arr))!=-1){access.write(arr,
0, len);threadclass.setFinished(threadclass.getFinished()+len); //每条下载线程进度的更新mfinished=mfinished+len; //总的进度下载//每隔1秒发送进度if(System.currentTimeMillis()-time>1000){time=System.currentTimeMillis();intent2.putExtra("finished", mfinished*100/soft.getLength()); //进度更新intent2.putExtra("id",
soft.getId()); //判断那个文件下载conn.sendBroadcast(intent2); //发送广播}//如果标签为true直接把下载循环暂停if(isDown){sqlservice.updataThreadClass(threadclass.getFinished(), threadclass.getId(), threadclass.getUrl());return;}}//如果该线程下载完毕那么该标签就变成trueisFinished=true;//监听每条线程下载资源是否完成checkAllThreadFinished();}}
catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {http.disconnect(); //断开连接access.close();in.close();} catch (Exception e) {//
TODO Auto-generated catch blocke.printStackTrace();}}}}
androdid 断点续传
最新推荐文章于 2025-08-19 16:03:58 发布