PC版与Android手机版带断点续传的多线程下载
程序员文章站
2023-11-04 13:23:34
一、多线程下载
多线程下载就是抢占服务器资源
&nbs...
一、多线程下载
多线程下载就是抢占服务器资源
原理:服务器cpu 分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服务器资源。
1、设置开启线程数,发送http请求到下载地址,获取下载文件的总长度
然后创建一个长度一致的临时文件,避免下载到一半存储空间不够了,并计算每个线程下载多少数据
2、计算每个线程下载数据的开始和结束位置
再次发送请求,用 range 头请求开始位置和结束位置的数据
3、将下载到的数据,存放至临时文件中
4、带断点续传的多线程下载
定义一个int变量,记录每条线程下载的数据总长度,然后加上该线程的下载开始位置,得到的结果就是下次下载时,该线程的开始位置,把得到的结果存入缓存文件,当文件下载完成,删除临时进度文件。
public class multidownload { static int threadcount = ; static int finishedthread = ; //确定下载地址 static string filename = "editplus.exe"; static string path = "http://...:/"+filename; public static void main(string[] args) { //、发送get请求,去获得下载文件的长度 try { url url = new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setconnecttimeout(); conn.setreadtimeout(); if (conn.getresponsecode()==) { //如果请求成功,拿到所请求资源文件的长度 int length = conn.getcontentlength(); //、生成一个与原文件同样的大小的临时文件,以免下载一半存储空间不够了 file file = new file(filename);//演示,所以将保存的文件目录放在工程的同目录 //使用randomaccessfile 生成临时文件,可以用指针定位文件的任意位置, //而且能够实时写到硬件底层设备,略过缓存,这对下载文件是突然断电等意外是有好处的 randomaccessfile raf = new randomaccessfile(file, "rwd");//rwd, 实时写到底层设备 //设置临时文件的大小 raf.setlength(length); raf.close(); //、计算出每个线程应该下载多少个字节 int size = length/threadcount;//如果有余数,负责最后一部分的线程负责下砸 //开启多线程 for (int threadid = ; threadid < threadcount; threadid++) { //计算每个线程下载的开始位置和结束位置 int startindex = threadid*size; // 开始 = 线程id * size int endindex = (threadid+)*size - ; //结束 = (线程id + )*size - //如果是最后一个线程,那么结束位置写死为文件结束位置 if (threadid == threadcount - ) { endindex = length - ; } //system.out.println("线程"+threadid+"的下载区间是: "+startindex+"----"+endindex); new downloadthread(startindex,endindex,threadid).start(); } } } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } } class downloadthread extends thread{ private int startindex; private int endindex; private int threadid; public downloadthread(int startindex, int endindex, int threadid) { super(); this.startindex = startindex; this.endindex = endindex; this.threadid = threadid; } public void run() { //每个线程再次发送http请求,下载自己对应的那部分数据 try { file progressfile = new file(threadid+".txt"); //判断进度文件是否存在,如果存在,则接着断点继续下载,如果不存在,则从头下载 if (progressfile.exists()) { fileinputstream fis = new fileinputstream(progressfile); bufferedreader br = new bufferedreader(new inputstreamreader(fis)); //从进度文件中度取出上一次下载的总进度,然后与原本的开始进度相加,得到新的开始进度 startindex += integer.parseint(br.readline()); fis.close(); } system.out.println("线程"+threadid+"的下载区间是:"+startindex+"----"+endindex); //、每个线程发送http请求自己的数据 url url = new url(multidownload.path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setconnecttimeout(); conn.setreadtimeout(); //设置本次http请求所请求的数据的区间 conn.setrequestproperty("range", "bytes="+startindex+"-"+endindex); //请求部分数据,响应码是 if (conn.getresponsecode()==) { //此时,流里只有threadcount分之一的原文件数据 inputstream is = conn.getinputstream(); byte[] b = new byte[]; int len = ; int total = ;//total 用于保存断点续传的断点 //拿到临时文件的输出流 file file = new file(multidownload.filename); randomaccessfile raf = new randomaccessfile(file, "rwd"); //把文件的写入位置移动至 startindex raf.seek(startindex); while ((len = is.read(b))!=-) { //每次读取流里数据之后,同步把数据写入临时文件 raf.write(b, , len); total += len; //system.out.println("线程" + threadid + "下载了" + total); //生成一个一个专门用来记录下载进度的临时文件 randomaccessfile progressraf = new randomaccessfile(progressfile, "rwd"); progressraf.write((total+"").getbytes()); progressraf.close(); } system.out.println("线程"+threadid+"下载完了---------------------"); raf.close(); //当所有的线程下载完之后,将进度文件删除 multidownload.finishedthread++; synchronized (multidownload.path) {//所有线程使用同一个锁 if (multidownload.finishedthread==multidownload.threadcount) { for (int i = ; i < multidownload.threadcount; i++) { file f = new file(i+".txt"); f.delete(); } multidownload.finishedthread=; } } } } catch (exception e) { e.printstacktrace(); } } }
二、android手机版带断点续传的多线程下载
android手机版的带断点续传的多线程下载逻辑与pc版的几乎一样,只不过在android手机中耗时操作不能放在主线程,网络下载属于耗时操作,所以多线程下载要在android中开启一个子线程执行。并使用消息队列机制刷新文本进度条。
public class mainactivity extends activity { static int threadcount = ; static int finishedthread = ; int currentprogess; static string filename = "qqplayer.exe"; static string path = "http://...:/"+filename; static mainactivity ma; static progressbar pb; static textview tv; static handler handler = new handler(){ public void handlemessage(android.os.message msg){ tv.settext((long)pb.getprogress()* /pb.getmax() +"%"); }; }; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ma = this; pb = (progressbar) findviewbyid(r.id.pb); tv = (textview) findviewbyid(r.id.tv); } public void download(view v){ thread t = new thread(){ public void run() { //发送http请求获取文件的长度,创建临时文件 try { url url= new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setconnecttimeout(); conn.setreadtimeout(); if (conn.getresponsecode()==) { int length = conn.getcontentlength(); //设置进度条的最大值就是原文件的总长度 pb.setmax(length); //生成一个与原文件相同大小的临时文件 file file = new file(environment.getexternalstoragedirectory(),filename); randomaccessfile raf = new randomaccessfile(file, "rwd"); raf.setlength(length); raf.close(); //计算每个线程需要下载的数据大小 int size = length/threadcount; //开启多线程 for (int threadid = ; threadid < threadcount; threadid++) { int startindex = threadid*size; int endindex = (threadid + )*size - ; if (threadid==threadcount - ) { endindex = length - ; } new downloadthread(startindex, endindex, threadid).start(); } } } catch (exception e) { e.printstacktrace(); } } }; t.start(); } class downloadthread extends thread{ private int startindex; private int endindex; private int threadid; public downloadthread(int startindex, int endindex, int threadid) { super(); this.startindex = startindex; this.endindex = endindex; this.threadid = threadid; } public void run() { // 每个线程发送http请求自己的数据 try{ //先判断是不是断点续传 file progessfile = new file(environment.getexternalstoragedirectory(),threadid+".txt"); if (progessfile.exists()) { filereader fr = new filereader(progessfile); bufferedreader br = new bufferedreader(fr); int lastprogess = integer.parseint(br.readline()); startindex += lastprogess; //把上次下载的进度显示至进度条 currentprogess +=lastprogess; pb.setprogress(currentprogess); //发消息,让主线程刷新文本进度 handler.sendemptymessage(); br.close(); fr.close(); } url url = new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setconnecttimeout(); conn.setreadtimeout(); conn.setrequestproperty("range", "bytes="+startindex+"-"+endindex); if (conn.getresponsecode()==) { inputstream is = conn.getinputstream(); byte[] buffer = new byte[]; int len = ; int total = ; file file = new file(environment.getexternalstoragedirectory(),filename); randomaccessfile raf = new randomaccessfile(file, "rwd"); raf.seek(startindex); while ((len = is.read(buffer))!= -) { raf.write(buffer, , len); total += len; //每次读取流里数据之后,把本次读取的数据的长度显示至进度条 currentprogess += len; pb.setprogress(currentprogess); //发消息,让主线程刷新文本进度 handler.sendemptymessage(); //生成临时文件保存下载进度,用于断点续传,在所有线程现在完毕后删除临时文件 randomaccessfile progressraf = new randomaccessfile(progessfile, "rwd"); progressraf.write((total+"").getbytes()); progressraf.close(); } raf.close(); system.out.println("线程"+threadid+"下载完了"); //当所有线程都下在完了之后,删除临时进度文件 finishedthread++; synchronized (path) { if (finishedthread==threadcount) { for (int i = ; i < threadcount; i++) { file f = new file(environment.getexternalstoragedirectory(),i+".txt"); f.delete(); } finishedthread=; } } } } catch (exception e) { e.printstacktrace(); } } } }
以上内容是小编跟大家分享的pc版与android手机版带断点续传的多线程下载,希望大家喜欢。
下一篇: 两个重叠的div做前后翻转