|
|
@ -4,9 +4,7 @@ import android.content.Context; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.SparseArray; |
|
|
|
import android.util.SparseArray; |
|
|
|
|
|
|
|
|
|
|
|
import com.arialyy.downloadutil.entity.DownloadEntity; |
|
|
|
import com.arialyy.downloadutil.entity.DownloadEntity; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
@ -22,6 +20,7 @@ import java.util.Properties; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
final class DownLoadUtil { |
|
|
|
final class DownLoadUtil { |
|
|
|
private static final String TAG = "DownLoadUtil"; |
|
|
|
private static final String TAG = "DownLoadUtil"; |
|
|
|
|
|
|
|
private static final Object LOCK = new Object(); |
|
|
|
//下载监听
|
|
|
|
//下载监听
|
|
|
|
private IDownloadListener mListener; |
|
|
|
private IDownloadListener mListener; |
|
|
|
/** |
|
|
|
/** |
|
|
@ -43,7 +42,6 @@ final class DownLoadUtil { |
|
|
|
private Context mContext; |
|
|
|
private Context mContext; |
|
|
|
private DownloadEntity mDownloadEntity; |
|
|
|
private DownloadEntity mDownloadEntity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DownLoadUtil(Context context, DownloadEntity entity) { |
|
|
|
public DownLoadUtil(Context context, DownloadEntity entity) { |
|
|
|
mContext = context.getApplicationContext(); |
|
|
|
mContext = context.getApplicationContext(); |
|
|
|
mDownloadEntity = entity; |
|
|
|
mDownloadEntity = entity; |
|
|
@ -84,8 +82,8 @@ final class DownLoadUtil { |
|
|
|
public void delConfigFile() { |
|
|
|
public void delConfigFile() { |
|
|
|
if (mContext != null && mDownloadEntity != null) { |
|
|
|
if (mContext != null && mDownloadEntity != null) { |
|
|
|
File dFile = new File(mDownloadEntity.getDownloadPath()); |
|
|
|
File dFile = new File(mDownloadEntity.getDownloadPath()); |
|
|
|
File config = new File( |
|
|
|
File config = |
|
|
|
mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); |
|
|
|
new File(mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); |
|
|
|
if (config.exists()) { |
|
|
|
if (config.exists()) { |
|
|
|
config.delete(); |
|
|
|
config.delete(); |
|
|
|
} |
|
|
|
} |
|
|
@ -120,8 +118,8 @@ final class DownLoadUtil { |
|
|
|
final String downloadUrl = mDownloadEntity.getDownloadUrl(); |
|
|
|
final String downloadUrl = mDownloadEntity.getDownloadUrl(); |
|
|
|
final File dFile = new File(filePath); |
|
|
|
final File dFile = new File(filePath); |
|
|
|
//读取已完成的线程数
|
|
|
|
//读取已完成的线程数
|
|
|
|
final File configFile = new File( |
|
|
|
final File configFile = |
|
|
|
mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); |
|
|
|
new File(mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (!configFile.exists()) { //记录文件被删除,则重新下载
|
|
|
|
if (!configFile.exists()) { //记录文件被删除,则重新下载
|
|
|
|
isNewTask = true; |
|
|
|
isNewTask = true; |
|
|
@ -142,11 +140,11 @@ final class DownLoadUtil { |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
conn.setRequestMethod("GET"); |
|
|
|
conn.setRequestMethod("GET"); |
|
|
|
conn.setRequestProperty("Charset", "UTF-8"); |
|
|
|
conn.setRequestProperty("Charset", "UTF-8"); |
|
|
|
|
|
|
|
conn.setConnectTimeout(TIME_OUT * 4); |
|
|
|
conn.setRequestProperty("User-Agent", |
|
|
|
conn.setRequestProperty("User-Agent", |
|
|
|
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); |
|
|
|
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); |
|
|
|
conn.setRequestProperty("Accept", |
|
|
|
conn.setRequestProperty("Accept", |
|
|
|
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); |
|
|
|
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); |
|
|
|
conn.setConnectTimeout(TIME_OUT * 4); |
|
|
|
|
|
|
|
conn.connect(); |
|
|
|
conn.connect(); |
|
|
|
int len = conn.getContentLength(); |
|
|
|
int len = conn.getContentLength(); |
|
|
|
if (len < 0) { //网络被劫持时会出现这个问题
|
|
|
|
if (len < 0) { //网络被劫持时会出现这个问题
|
|
|
@ -207,8 +205,9 @@ final class DownLoadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
//分配下载位置
|
|
|
|
//分配下载位置
|
|
|
|
Object record = pro.getProperty(dFile.getName() + "_record_" + i); |
|
|
|
Object record = pro.getProperty(dFile.getName() + "_record_" + i); |
|
|
|
if (!isNewTask && record != null && Long.parseLong( |
|
|
|
if (!isNewTask |
|
|
|
record + "") > 0) { //如果有记录,则恢复下载
|
|
|
|
&& 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 + "_恢复下载 ++++++++++"); |
|
|
@ -227,9 +226,8 @@ final class DownLoadUtil { |
|
|
|
//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
|
|
|
//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
|
|
|
endL = fileLength; |
|
|
|
endL = fileLength; |
|
|
|
} |
|
|
|
} |
|
|
|
ConfigEntity entity = new ConfigEntity(mContext, fileLength, |
|
|
|
ConfigEntity entity = |
|
|
|
downloadUrl, dFile, i, startL, |
|
|
|
new ConfigEntity(mContext, fileLength, downloadUrl, dFile, i, startL, endL); |
|
|
|
endL); |
|
|
|
|
|
|
|
DownLoadTask task = new DownLoadTask(entity); |
|
|
|
DownLoadTask task = new DownLoadTask(entity); |
|
|
|
tasks.put(i, new Thread(task)); |
|
|
|
tasks.put(i, new Thread(task)); |
|
|
|
} |
|
|
|
} |
|
|
@ -249,9 +247,12 @@ final class DownLoadUtil { |
|
|
|
failDownload("下载失败,返回码:" + code); |
|
|
|
failDownload("下载失败,返回码:" + code); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
failDownload( |
|
|
|
failDownload("下载失败【downloadUrl:" |
|
|
|
"下载失败【downloadUrl:" + downloadUrl + "】\n【filePath:" + filePath + "】" + Util |
|
|
|
+ downloadUrl |
|
|
|
.getPrintException(e)); |
|
|
|
+ "】\n【filePath:" |
|
|
|
|
|
|
|
+ filePath |
|
|
|
|
|
|
|
+ "】" |
|
|
|
|
|
|
|
+ Util.getPrintException(e)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}).start(); |
|
|
|
}).start(); |
|
|
@ -272,17 +273,25 @@ final class DownLoadUtil { |
|
|
|
private ConfigEntity dEntity; |
|
|
|
private ConfigEntity dEntity; |
|
|
|
private String configFPath; |
|
|
|
private String configFPath; |
|
|
|
|
|
|
|
|
|
|
|
public DownLoadTask(ConfigEntity downloadInfo) { |
|
|
|
private DownLoadTask(ConfigEntity downloadInfo) { |
|
|
|
this.dEntity = downloadInfo; |
|
|
|
this.dEntity = downloadInfo; |
|
|
|
configFPath = dEntity.context.getFilesDir() |
|
|
|
configFPath = dEntity.context.getFilesDir().getPath() |
|
|
|
.getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties"; |
|
|
|
+ "/temp/" |
|
|
|
|
|
|
|
+ dEntity.tempFile.getName() |
|
|
|
|
|
|
|
+ ".properties"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void run() { |
|
|
|
@Override public void run() { |
|
|
|
long currentLocation = 0; |
|
|
|
long currentLocation = 0; |
|
|
|
try { |
|
|
|
try { |
|
|
|
Log.d(TAG, |
|
|
|
Log.d(TAG, "线程_" |
|
|
|
"线程_" + dEntity.threadId + "_正在下载【" + "开始位置 : " + dEntity.startLocation + ",结束位置:" + dEntity.endLocation + "】"); |
|
|
|
+ dEntity.threadId |
|
|
|
|
|
|
|
+ "_正在下载【" |
|
|
|
|
|
|
|
+ "开始位置 : " |
|
|
|
|
|
|
|
+ dEntity.startLocation |
|
|
|
|
|
|
|
+ ",结束位置:" |
|
|
|
|
|
|
|
+ dEntity.endLocation |
|
|
|
|
|
|
|
+ "】"); |
|
|
|
URL url = new URL(dEntity.downloadUrl); |
|
|
|
URL url = new URL(dEntity.downloadUrl); |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
//在头里面请求下载开始位置和结束位置
|
|
|
|
//在头里面请求下载开始位置和结束位置
|
|
|
@ -315,48 +324,19 @@ final class DownLoadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
//把下载数据数据写入文件
|
|
|
|
//把下载数据数据写入文件
|
|
|
|
file.write(buffer, 0, len); |
|
|
|
file.write(buffer, 0, len); |
|
|
|
synchronized (DownLoadUtil.this) { |
|
|
|
progress(len); |
|
|
|
mCurrentLocation += len; |
|
|
|
|
|
|
|
mListener.onProgress(mCurrentLocation); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
currentLocation += len; |
|
|
|
currentLocation += len; |
|
|
|
} |
|
|
|
} |
|
|
|
file.close(); |
|
|
|
file.close(); |
|
|
|
is.close(); |
|
|
|
is.close(); |
|
|
|
|
|
|
|
|
|
|
|
if (isCancel) { |
|
|
|
if (isCancel) { |
|
|
|
synchronized (DownLoadUtil.this) { |
|
|
|
cancel(); |
|
|
|
mCancelNum++; |
|
|
|
|
|
|
|
if (mCancelNum == THREAD_NUM) { |
|
|
|
|
|
|
|
File configFile = new File(configFPath); |
|
|
|
|
|
|
|
if (configFile.exists()) { |
|
|
|
|
|
|
|
configFile.delete(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (dEntity.tempFile.exists()) { |
|
|
|
|
|
|
|
dEntity.tempFile.delete(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++"); |
|
|
|
|
|
|
|
isDownloading = false; |
|
|
|
|
|
|
|
mListener.onCancel(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
//停止状态不需要删除记录文件
|
|
|
|
//停止状态不需要删除记录文件
|
|
|
|
if (isStop) { |
|
|
|
if (isStop) { |
|
|
|
synchronized (DownLoadUtil.this) { |
|
|
|
stop(currentLocation); |
|
|
|
mStopNum++; |
|
|
|
|
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
|
|
|
|
Log.i(TAG, |
|
|
|
|
|
|
|
"thread_" + dEntity.threadId + "_stop, stop location ==> " + currentLocation); |
|
|
|
|
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
|
|
|
|
location); |
|
|
|
|
|
|
|
if (mStopNum == THREAD_NUM) { |
|
|
|
|
|
|
|
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++"); |
|
|
|
|
|
|
|
isDownloading = false; |
|
|
|
|
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
Log.i(TAG, "线程【" + dEntity.threadId + "】下载完毕"); |
|
|
|
Log.i(TAG, "线程【" + dEntity.threadId + "】下载完毕"); |
|
|
@ -372,41 +352,58 @@ final class DownLoadUtil { |
|
|
|
mListener.onComplete(); |
|
|
|
mListener.onComplete(); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (MalformedURLException e) { |
|
|
|
} catch (MalformedURLException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
failDownload(dEntity, currentLocation, "下载链接异常", e); |
|
|
|
isDownloading = false; |
|
|
|
} catch (IOException e) { |
|
|
|
synchronized (DownLoadUtil.this) { |
|
|
|
failDownload(dEntity, currentLocation, "下载失败【" + dEntity.downloadUrl + "】", e); |
|
|
|
try { |
|
|
|
} catch (Exception e) { |
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
failDownload(dEntity, currentLocation, "获取流失败", e); |
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
|
|
|
|
location); |
|
|
|
|
|
|
|
failDownload("下载链接异常"); |
|
|
|
|
|
|
|
} catch (IOException e1) { |
|
|
|
|
|
|
|
e1.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
synchronized (DownLoadUtil.this) { |
|
|
|
/** |
|
|
|
try { |
|
|
|
* 停止下载 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IOException |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void stop(long currentLocation) throws IOException { |
|
|
|
|
|
|
|
synchronized (LOCK) { |
|
|
|
|
|
|
|
mStopNum++; |
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
Log.i(TAG, "thread_" + dEntity.threadId + "_stop, stop location ==> " + currentLocation); |
|
|
|
location); |
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, location); |
|
|
|
failDownload( |
|
|
|
if (mStopNum == THREAD_NUM) { |
|
|
|
"下载失败【" + dEntity.downloadUrl + "】" + Util.getPrintException(e)); |
|
|
|
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++"); |
|
|
|
} catch (IOException e1) { |
|
|
|
isDownloading = false; |
|
|
|
e1.printStackTrace(); |
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
} |
|
|
|
synchronized (DownLoadUtil.this) { |
|
|
|
|
|
|
|
try { |
|
|
|
/** |
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
* 取消下载 |
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
*/ |
|
|
|
location); |
|
|
|
private void cancel() { |
|
|
|
failDownload("获取流失败" + Util.getPrintException(e)); |
|
|
|
synchronized (LOCK) { |
|
|
|
} catch (IOException e1) { |
|
|
|
mCancelNum++; |
|
|
|
e1.printStackTrace(); |
|
|
|
if (mCancelNum == THREAD_NUM) { |
|
|
|
|
|
|
|
File configFile = new File(configFPath); |
|
|
|
|
|
|
|
if (configFile.exists()) { |
|
|
|
|
|
|
|
configFile.delete(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (dEntity.tempFile.exists()) { |
|
|
|
|
|
|
|
dEntity.tempFile.delete(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++"); |
|
|
|
|
|
|
|
isDownloading = false; |
|
|
|
|
|
|
|
mListener.onCancel(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void progress(long len) { |
|
|
|
|
|
|
|
synchronized (LOCK) { |
|
|
|
|
|
|
|
mCurrentLocation += len; |
|
|
|
|
|
|
|
mListener.onProgress(mCurrentLocation); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -419,6 +416,30 @@ final class DownLoadUtil { |
|
|
|
pro.setProperty(key, record); |
|
|
|
pro.setProperty(key, record); |
|
|
|
Util.saveConfig(configFile, pro); |
|
|
|
Util.saveConfig(configFile, pro); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 下载失败 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void failDownload(ConfigEntity dEntity, long currentLocation, String msg, |
|
|
|
|
|
|
|
Exception ex) { |
|
|
|
|
|
|
|
synchronized (LOCK) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
isDownloading = false; |
|
|
|
|
|
|
|
isStop = true; |
|
|
|
|
|
|
|
Log.e(TAG, msg); |
|
|
|
|
|
|
|
if (ex != null) { |
|
|
|
|
|
|
|
Log.e(TAG, Util.getPrintException(ex)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (currentLocation != -1) { |
|
|
|
|
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
|
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, location); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mListener.onFail(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -434,8 +455,8 @@ final class DownLoadUtil { |
|
|
|
File tempFile; |
|
|
|
File tempFile; |
|
|
|
Context context; |
|
|
|
Context context; |
|
|
|
|
|
|
|
|
|
|
|
public ConfigEntity(Context context, long fileSize, String downloadUrl, File file, |
|
|
|
public ConfigEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, |
|
|
|
int threadId, long startLocation, long endLocation) { |
|
|
|
long startLocation, long endLocation) { |
|
|
|
this.fileSize = fileSize; |
|
|
|
this.fileSize = fileSize; |
|
|
|
this.downloadUrl = downloadUrl; |
|
|
|
this.downloadUrl = downloadUrl; |
|
|
|
this.tempFile = file; |
|
|
|
this.tempFile = file; |
|
|
|