原理:获取目标文件的大小,在本地创建一个相同大小的文件,并计算每个线程需要下载的起始位置及大小,然后分配至每个线程独立下载,全部下载完毕则自动合并.
实现步骤
-
查看并计算目标文件的大小
URL url = new URL(mPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int code = connection.getResponseCode();
if (code == 200) {int length = connection.getContentLength();System.out.println("文件总长度为:" + length); -
设置目标文件在本地的映射
RandomAccessFile raf = new RandomAccessFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getDownlaodName(mPath), "rw");
raf.setLength(length); -
开启子线程并分配下载任务
int blokeSize = length / mTotalCount;
System.out.println("每一块大小为:" + blokeSize);
runningThreadCount = mTotalCount;
for (int threadid = 0; threadid -
子线程中的具体逻辑
public void run() {System.out.println("线程"&#43;threadid&#43;"开始运行了");try{//读存有历史下载进度的文件,判断是否已经下载过File finfo&#61;new File(Environment.getExternalStorageDirectory().getAbsolutePath()&#43;"/"&#43;mTotalCount &#43; getDownlaodName(mPath)&#43;threadid&#43;".txt");if (finfo.exists()&&finfo.length()>0) {//获取文件输入流FileInputStream fis&#61;new FileInputStream(finfo);//读取缓冲BufferedReader br&#61;new BufferedReader(new InputStreamReader(fis));//得到文件内容String lasrposition&#61;br.readLine();//转化为int值int intlastposition&#61;Integer.parseInt(lasrposition);lastLoadSize&#61;intlastposition-startPosition;startPosition&#61;intlastposition;fis.close();}URL url&#61;new URL(mPath);HttpURLConnection conn&#61;(HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");System.out.println("线程实际下载&#xff1a;"&#43;threadid&#43;"&#xff0c;范围&#xff1a;"&#43;startPosition&#43;"~~"&#43;endPosition);//设置http请求头部参数conn.setRequestProperty("Range", "bytes&#61;"&#43;startPosition&#43;"-"&#43;endPosition);int code&#61;conn.getResponseCode();//206代表请求部分数据成功if (code&#61;&#61;206) {//拿到链接返回的输入流InputStream is&#61;conn.getInputStream();//指向要写的文件&#xff08;abc.exe&#xff09;RandomAccessFile raf&#61;new RandomAccessFile(Environment.getExternalStorageDirectory().getAbsolutePath()&#43;"/"&#43;getDownlaodName(mPath), "rw");//指定文件开始写的位置raf.seek(startPosition);//下载缓冲区&#xff0c;越大下载越快&#xff0c;对硬盘损耗越小&#xff0c;但是越容易丢失数据byte[] buffer&#61;new byte[1024*1024];int len&#61;-1;int total&#61;0;//当前线程本次下载数据总量while ((len&#61;is.read(buffer))!&#61;-1) {raf.write(buffer,0,len);total&#43;&#61;len;//将每次更新的数据同步到底层硬盘RandomAccessFile inforaf&#61;new RandomAccessFile(Environment.getExternalStorageDirectory().getAbsolutePath()&#43;"/"&#43;mTotalCount &#43;getDownlaodName(mPath)&#43;threadid&#43;".txt","rwd");//保存当前线程下载到什么位置inforaf.write(String.valueOf(startPosition&#43;total).getBytes());inforaf.close();}is.close();raf.close();System.out.println("线程"&#43;threadid&#43;"下载完毕");}}catch(Exception e){e.printStackTrace();}finally{//同步代码块&#xff0c;保证同时间仅有一个线程执行此区块代码synchronized (MainActivity.class) {runningThreadCount--;if (runningThreadCount<&#61;0) {System.out.println("多线程下载完毕");for (int i &#61; 0; i
}
备注&#xff1a;如果不使用断点下载&#xff0c;只需要将判断和存储历史下载信息的逻辑删除即可。