|
|
|
@ -25,16 +25,16 @@ public class DownLoadUtil { |
|
|
|
|
/** |
|
|
|
|
* 线程数 |
|
|
|
|
*/ |
|
|
|
|
private static final int THREAD_NUM = 3; |
|
|
|
|
private static final int THREAD_NUM = 3; |
|
|
|
|
private static final int TIME_OUT = 5000; //超时时间
|
|
|
|
|
/** |
|
|
|
|
* 已经完成下载任务的线程数量 |
|
|
|
|
*/ |
|
|
|
|
private int mCompleteThreadNum = 0; |
|
|
|
|
private int mCompleteThreadNum = 0; |
|
|
|
|
private boolean isDownloading = false; |
|
|
|
|
private boolean isStop = false; |
|
|
|
|
private boolean isCancel = false; |
|
|
|
|
private long mCurrentLocation; |
|
|
|
|
private boolean isDownloading = false; |
|
|
|
|
private boolean isStop = false; |
|
|
|
|
private boolean isCancel = false; |
|
|
|
|
private static final int TIME_OUT = 5000; //超时时间
|
|
|
|
|
boolean isNewTask = true; |
|
|
|
|
private int mCancelNum = 0; |
|
|
|
|
private int mStopNum = 0; |
|
|
|
@ -114,9 +114,9 @@ public class DownLoadUtil { |
|
|
|
|
conn.setRequestProperty("Charset", "UTF-8"); |
|
|
|
|
conn.setConnectTimeout(TIME_OUT * 4); |
|
|
|
|
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", |
|
|
|
|
"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.connect(); |
|
|
|
|
int len = conn.getContentLength(); |
|
|
|
|
if (len < 0) { //网络被劫持时会出现这个问题
|
|
|
|
@ -197,7 +197,8 @@ public class DownLoadUtil { |
|
|
|
|
endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
|
|
|
|
} |
|
|
|
|
DownloadEntity entity = new DownloadEntity(context, fileLength, |
|
|
|
|
downloadUrl, dFile, i, startL, endL); |
|
|
|
|
downloadUrl, dFile, i, |
|
|
|
|
startL, endL); |
|
|
|
|
DownLoadTask task = new DownLoadTask(entity); |
|
|
|
|
tasks.put(i, new Thread(task)); |
|
|
|
|
} |
|
|
|
@ -230,7 +231,6 @@ public class DownLoadUtil { |
|
|
|
|
isDownloading = false; |
|
|
|
|
stopDownload(); |
|
|
|
|
mListener.onFail(); |
|
|
|
|
System.gc(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -244,26 +244,26 @@ public class DownLoadUtil { |
|
|
|
|
public DownLoadTask(DownloadEntity downloadInfo) { |
|
|
|
|
this.dEntity = downloadInfo; |
|
|
|
|
configFPath = dEntity.context.getFilesDir() |
|
|
|
|
.getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties"; |
|
|
|
|
.getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override public void run() { |
|
|
|
|
long currentLocation = 0; |
|
|
|
|
try { |
|
|
|
|
Log.d(TAG, |
|
|
|
|
"线程_" + dEntity.threadId + "_正在下载【" + "开始位置 : " + dEntity.startLocation + ",结束位置:" + dEntity.endLocation + "】"); |
|
|
|
|
"线程_" + dEntity.threadId + "_正在下载【" + "开始位置 : " + dEntity.startLocation + ",结束位置:" + dEntity.endLocation + "】"); |
|
|
|
|
URL url = new URL(dEntity.downloadUrl); |
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
|
//在头里面请求下载开始位置和结束位置
|
|
|
|
|
conn.setRequestProperty("Range", |
|
|
|
|
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation); |
|
|
|
|
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation); |
|
|
|
|
conn.setRequestMethod("GET"); |
|
|
|
|
conn.setRequestProperty("Charset", "UTF-8"); |
|
|
|
|
conn.setConnectTimeout(TIME_OUT * 4); |
|
|
|
|
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", |
|
|
|
|
"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.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数
|
|
|
|
|
InputStream is = conn.getInputStream(); |
|
|
|
|
//创建可设置位置的文件
|
|
|
|
@ -306,7 +306,6 @@ public class DownLoadUtil { |
|
|
|
|
Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++"); |
|
|
|
|
isDownloading = false; |
|
|
|
|
mListener.onCancel(); |
|
|
|
|
System.gc(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
@ -317,14 +316,13 @@ public class DownLoadUtil { |
|
|
|
|
mStopNum++; |
|
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
|
Log.i(TAG, |
|
|
|
|
"thread_" + dEntity.threadId + "_stop, stop location ==> " + currentLocation); |
|
|
|
|
"thread_" + dEntity.threadId + "_stop, stop location ==> " + currentLocation); |
|
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
|
location); |
|
|
|
|
location); |
|
|
|
|
if (mStopNum == THREAD_NUM) { |
|
|
|
|
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++"); |
|
|
|
|
isDownloading = false; |
|
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
|
System.gc(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
@ -340,7 +338,6 @@ public class DownLoadUtil { |
|
|
|
|
} |
|
|
|
|
isDownloading = false; |
|
|
|
|
mListener.onComplete(); |
|
|
|
|
System.gc(); |
|
|
|
|
} |
|
|
|
|
} catch (MalformedURLException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
@ -349,7 +346,7 @@ public class DownLoadUtil { |
|
|
|
|
try { |
|
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
|
location); |
|
|
|
|
location); |
|
|
|
|
failDownload("下载链接异常"); |
|
|
|
|
} catch (IOException e1) { |
|
|
|
|
e1.printStackTrace(); |
|
|
|
@ -360,7 +357,7 @@ public class DownLoadUtil { |
|
|
|
|
try { |
|
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
|
location); |
|
|
|
|
location); |
|
|
|
|
failDownload( |
|
|
|
|
"下载失败【" + dEntity.downloadUrl + "】" + Util.getPrintException(e)); |
|
|
|
|
} catch (IOException e1) { |
|
|
|
@ -372,7 +369,7 @@ public class DownLoadUtil { |
|
|
|
|
try { |
|
|
|
|
String location = String.valueOf(currentLocation); |
|
|
|
|
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId, |
|
|
|
|
location); |
|
|
|
|
location); |
|
|
|
|
failDownload("获取流失败" + Util.getPrintException(e)); |
|
|
|
|
} catch (IOException e1) { |
|
|
|
|
e1.printStackTrace(); |
|
|
|
|