pull/2/head
lyy 9 years ago
commit bcdff18f55
  1. 3
      README.md
  2. 38
      downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java

@ -1,5 +1,6 @@
# DownloadUtil # DownloadUtil
这是android 文件下载工具类,实现了多线程断点续传功能 这是android 文件下载工具类,实现了多线程断点续传功能</br>
如果你觉得我的代码对你有帮助,请麻烦你在右上角给我一个star.^_^
#下载 #下载
[![Download](https://api.bintray.com/packages/arialyy/maven/MTDownloadUtil/images/download.svg)](https://bintray.com/arialyy/maven/MTDownloadUtil/_latestVersion)<br/> [![Download](https://api.bintray.com/packages/arialyy/maven/MTDownloadUtil/images/download.svg)](https://bintray.com/arialyy/maven/MTDownloadUtil/_latestVersion)<br/>

@ -37,7 +37,7 @@ public class DownLoadUtil {
private boolean isStop = false; private boolean isStop = false;
private boolean isCancel = false; private boolean isCancel = false;
private static final int TIME_OUT = 5000; //超时时间 private static final int TIME_OUT = 5000; //超时时间
boolean newTask = true; boolean isNewTask = true;
private int mCancelNum = 0; private int mCancelNum = 0;
private int mStopNum = 0; private int mStopNum = 0;
@ -92,17 +92,16 @@ public class DownLoadUtil {
final File configFile = new File(context.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); final File configFile = new File(context.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties");
try { try {
if (!configFile.exists()) { //记录文件被删除,则重新下载 if (!configFile.exists()) { //记录文件被删除,则重新下载
newTask = true; isNewTask = true;
Util.createFile(configFile.getPath()); Util.createFile(configFile.getPath());
} else { } else {
newTask = false; isNewTask = !dFile.exists();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
mListener.onFail(); mListener.onFail();
return; return;
} }
newTask = !dFile.exists();
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -133,8 +132,24 @@ public class DownLoadUtil {
//分配每条线程的下载区间 //分配每条线程的下载区间
Properties pro = null; Properties pro = null;
pro = Util.loadConfig(configFile); pro = Util.loadConfig(configFile);
if (pro.isEmpty()) {
isNewTask = true;
} else {
for (int i = 0; i < THREAD_NUM; i++) {
if(pro.getProperty(dFile.getName() + "_record_" + i) == null){
isNewTask = true;
break;
}
}
}
int blockSize = fileLength / THREAD_NUM; int blockSize = fileLength / THREAD_NUM;
SparseArray<Thread> tasks = new SparseArray<>(); SparseArray<Thread> tasks = new SparseArray<>();
int[] recordL = new int[THREAD_NUM];
int rl = 0;
for (int i = 0; i < THREAD_NUM; i++) {
recordL[i] = -1;
}
for (int i = 0; i < THREAD_NUM; i++) { for (int i = 0; i < THREAD_NUM; i++) {
long startL = i * blockSize, endL = (i + 1) * blockSize; long startL = i * blockSize, endL = (i + 1) * blockSize;
Object state = pro.getProperty(dFile.getName() + "_state_" + i); Object state = pro.getProperty(dFile.getName() + "_state_" + i);
@ -142,6 +157,8 @@ public class DownLoadUtil {
mCurrentLocation += endL - startL; mCurrentLocation += endL - startL;
Log.d(TAG, "++++++++++ 线程_" + i + "_已经下载完成 ++++++++++"); Log.d(TAG, "++++++++++ 线程_" + i + "_已经下载完成 ++++++++++");
mCompleteThreadNum++; mCompleteThreadNum++;
mStopNum++;
mCancelNum++;
if (mCompleteThreadNum == THREAD_NUM) { if (mCompleteThreadNum == THREAD_NUM) {
if (configFile.exists()) { if (configFile.exists()) {
configFile.delete(); configFile.delete();
@ -155,12 +172,18 @@ public class DownLoadUtil {
} }
//分配下载位置 //分配下载位置
Object record = pro.getProperty(dFile.getName() + "_record_" + i); Object record = pro.getProperty(dFile.getName() + "_record_" + i);
if (!newTask && record != null && Long.parseLong(record + "") > 0) { //如果有记录,则恢复下载 if (!isNewTask && record != null && Long.parseLong(record + "") > 0) { //如果有记录,则恢复下载
Long r = Long.parseLong(record + ""); Long r = Long.parseLong(record + "");
mCurrentLocation += r - startL; mCurrentLocation += r - startL;
Log.d(TAG, "++++++++++ 线程_" + i + "_恢复下载 ++++++++++"); Log.d(TAG, "++++++++++ 线程_" + i + "_恢复下载 ++++++++++");
mListener.onChildResume(r); mListener.onChildResume(r);
startL = r; startL = r;
recordL[rl] = i;
rl++;
}
if (isNewTask) {
recordL[rl] = i;
rl++;
} }
if (i == (THREAD_NUM - 1)) { if (i == (THREAD_NUM - 1)) {
endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度 endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
@ -174,8 +197,9 @@ public class DownLoadUtil {
} else { } else {
mListener.onStart(mCurrentLocation); mListener.onStart(mCurrentLocation);
} }
for (int i = 0, count = tasks.size(); i < count; i++) { for (int l : recordL) {
Thread task = tasks.get(i); if (l == -1) continue;
Thread task = tasks.get(l);
if (task != null) { if (task != null) {
task.start(); task.start();
} }

Loading…
Cancel
Save