|
|
@ -13,6 +13,7 @@ import java.net.HttpURLConnection; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URL; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Created by lyy on 2015/8/25. |
|
|
|
* Created by lyy on 2015/8/25. |
|
|
|
* 下载工具类 |
|
|
|
* 下载工具类 |
|
|
@ -37,11 +38,14 @@ public class DownLoadUtil { |
|
|
|
boolean isNewTask = true; |
|
|
|
boolean isNewTask = true; |
|
|
|
private int mCancelNum = 0; |
|
|
|
private int mCancelNum = 0; |
|
|
|
private int mStopNum = 0; |
|
|
|
private int mStopNum = 0; |
|
|
|
|
|
|
|
|
|
|
|
public DownLoadUtil() { |
|
|
|
public DownLoadUtil() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public IDownloadListener getListener() { |
|
|
|
public IDownloadListener getListener() { |
|
|
|
return mListener; |
|
|
|
return mListener; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取当前下载位置 |
|
|
|
* 获取当前下载位置 |
|
|
|
* |
|
|
|
* |
|
|
@ -50,21 +54,25 @@ public class DownLoadUtil { |
|
|
|
public long getCurrentLocation() { |
|
|
|
public long getCurrentLocation() { |
|
|
|
return mCurrentLocation; |
|
|
|
return mCurrentLocation; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean isDownloading() { |
|
|
|
public boolean isDownloading() { |
|
|
|
return isDownloading; |
|
|
|
return isDownloading; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 取消下载 |
|
|
|
* 取消下载 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void cancelDownload() { |
|
|
|
public void cancelDownload() { |
|
|
|
isCancel = true; |
|
|
|
isCancel = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 停止下载 |
|
|
|
* 停止下载 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void stopDownload() { |
|
|
|
public void stopDownload() { |
|
|
|
isStop = true; |
|
|
|
isStop = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 多线程断点续传下载文件,暂停和继续 |
|
|
|
* 多线程断点续传下载文件,暂停和继续 |
|
|
|
* |
|
|
|
* |
|
|
@ -211,6 +219,7 @@ public class DownLoadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
}).start(); |
|
|
|
}).start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void failDownload(String msg) { |
|
|
|
private void failDownload(String msg) { |
|
|
|
Log.e(TAG, msg); |
|
|
|
Log.e(TAG, msg); |
|
|
|
isDownloading = false; |
|
|
|
isDownloading = false; |
|
|
@ -218,6 +227,7 @@ public class DownLoadUtil { |
|
|
|
mListener.onFail(); |
|
|
|
mListener.onFail(); |
|
|
|
System.gc(); |
|
|
|
System.gc(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 多线程下载任务类,不能使用AsyncTask来进行多线程下载,因为AsyncTask是串行执行的,这种方式下载速度太慢了 |
|
|
|
* 多线程下载任务类,不能使用AsyncTask来进行多线程下载,因为AsyncTask是串行执行的,这种方式下载速度太慢了 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -225,10 +235,12 @@ public class DownLoadUtil { |
|
|
|
private static final String TAG = "DownLoadTask"; |
|
|
|
private static final String TAG = "DownLoadTask"; |
|
|
|
private DownloadEntity dEntity; |
|
|
|
private DownloadEntity dEntity; |
|
|
|
private String configFPath; |
|
|
|
private String configFPath; |
|
|
|
|
|
|
|
|
|
|
|
public DownLoadTask(DownloadEntity downloadInfo) { |
|
|
|
public DownLoadTask(DownloadEntity downloadInfo) { |
|
|
|
this.dEntity = downloadInfo; |
|
|
|
this.dEntity = downloadInfo; |
|
|
|
configFPath = dEntity.context.getFilesDir().getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties"; |
|
|
|
configFPath = dEntity.context.getFilesDir().getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
long currentLocation = 0; |
|
|
|
long currentLocation = 0; |
|
|
@ -353,6 +365,7 @@ public class DownLoadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 将记录写入到配置文件 |
|
|
|
* 将记录写入到配置文件 |
|
|
|
* |
|
|
|
* |
|
|
@ -365,6 +378,7 @@ public class DownLoadUtil { |
|
|
|
Util.saveConfig(configFile, pro); |
|
|
|
Util.saveConfig(configFile, pro); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 子线程下载信息类 |
|
|
|
* 子线程下载信息类 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -377,6 +391,7 @@ public class DownLoadUtil { |
|
|
|
long endLocation; |
|
|
|
long endLocation; |
|
|
|
File tempFile; |
|
|
|
File tempFile; |
|
|
|
Context context; |
|
|
|
Context context; |
|
|
|
|
|
|
|
|
|
|
|
public DownloadEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, long startLocation, long endLocation) { |
|
|
|
public DownloadEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, long startLocation, long endLocation) { |
|
|
|
this.fileSize = fileSize; |
|
|
|
this.fileSize = fileSize; |
|
|
|
this.downloadUrl = downloadUrl; |
|
|
|
this.downloadUrl = downloadUrl; |
|
|
|