pull/330/head
AriaLyy 8 years ago
parent fd5b4f0248
commit e71729bd09
  1. 66
      Aria/src/main/java/com/arialyy/aria/core/download/ConnectionHelp.java
  2. 19
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  3. 2
      app/src/main/assets/aria_config.xml
  4. 3
      app/src/main/java/com/arialyy/simple/test/TestMutilTaskSysDownload.java

@ -63,38 +63,50 @@ class ConnectionHelp {
static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn) static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn)
throws ProtocolException { throws ProtocolException {
conn.setRequestMethod(entity.requestEnum.name); conn.setRequestMethod(entity.requestEnum.name);
Set<String> keys = null;
if (entity.headers != null && entity.headers.size() > 0) { if (entity.headers != null && entity.headers.size() > 0) {
Set<String> keys = entity.headers.keySet(); keys = entity.headers.keySet();
for (String key : keys) { for (String key : keys) {
conn.setRequestProperty(key, entity.headers.get(key)); conn.setRequestProperty(key, entity.headers.get(key));
} }
} }
conn.setRequestProperty("Charset", "UTF-8"); if (keys == null || !keys.contains("Charset")) {
conn.setRequestProperty("User-Agent", conn.setRequestProperty("Charset", "UTF-8");
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); }
StringBuilder accept = new StringBuilder(); if (keys == null || !keys.contains("User-Agent")) {
accept.append("image/gif, ") conn.setRequestProperty("User-Agent",
.append("image/jpeg, ") "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
.append("image/pjpeg, ") }
.append("image/webp, ") if (keys == null || !keys.contains("Accept")) {
.append("image/*, ") StringBuilder accept = new StringBuilder();
.append("application/xml") accept.append("image/gif, ")
.append("application/xaml+xml, ") .append("image/jpeg, ")
.append("application/xhtml+xml, ") .append("image/pjpeg, ")
.append("application/x-shockwave-flash, ") .append("image/webp, ")
.append("application/x-ms-xbap, ") .append("application/xml, ")
.append("application/x-ms-application, ") .append("application/xaml+xml, ")
.append("application/msword, ") .append("application/xhtml+xml, ")
.append("application/vnd.ms-excel, ") .append("application/x-shockwave-flash, ")
.append("application/vnd.ms-xpsdocument, ") .append("application/x-ms-xbap, ")
.append("application/vnd.ms-powerpoint, ") .append("application/x-ms-application, ")
.append("text/plain, ") .append("application/msword, ")
.append("text/html, ") .append("application/vnd.ms-excel, ")
.append("*/*"); .append("application/vnd.ms-xpsdocument, ")
conn.setRequestProperty("Accept", accept.toString()); .append("application/vnd.ms-powerpoint, ")
conn.setRequestProperty("Accept-Encoding", "identity"); .append("text/plain, ")
conn.setRequestProperty("Accept-Charset", "UTF-8"); .append("text/html, ")
conn.setRequestProperty("Connection", "Keep-Alive"); .append("*/*");
conn.setRequestProperty("Accept", accept.toString());
}
if (keys == null || !keys.contains("Accept-Encoding")) {
conn.setRequestProperty("Accept-Encoding", "identity");
}
if (keys == null || !keys.contains("Accept-Charset")) {
conn.setRequestProperty("Accept-Charset", "UTF-8");
}
if (keys == null || !keys.contains("Connection")) {
conn.setRequestProperty("Connection", "Keep-Alive");
}
//302获取重定向地址 //302获取重定向地址
conn.setInstanceFollowRedirects(false); conn.setInstanceFollowRedirects(false);
return conn; return conn;

@ -81,7 +81,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
+ ".properties"); + ".properties");
try { try {
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载 if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
handleNewTask(); isNewTask = true;
CommonUtil.createFile(mConfigFile.getPath()); CommonUtil.createFile(mConfigFile.getPath());
} else { } else {
isNewTask = !mDownloadFile.exists(); isNewTask = !mDownloadFile.exists();
@ -295,10 +295,6 @@ class DownloadUtil implements IDownloadUtil, Runnable {
return; return;
} }
int fileLength = conn.getContentLength(); int fileLength = conn.getContentLength();
if (fileLength < SUB_LEN) {
THREAD_NUM = 1;
CONSTANCE.THREAD_NUM = THREAD_NUM;
}
Properties pro = createConfigFile(fileLength); Properties pro = createConfigFile(fileLength);
int blockSize = fileLength / THREAD_NUM; int blockSize = fileLength / THREAD_NUM;
int[] recordL = new int[THREAD_NUM]; int[] recordL = new int[THREAD_NUM];
@ -325,7 +321,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
recordL[rl] = i; recordL[rl] = i;
rl++; rl++;
} else { } else {
handleNewTask(); handleNewTask(fileLength);
} }
if (isNewTask) { if (isNewTask) {
recordL[rl] = i; recordL[rl] = i;
@ -380,7 +376,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
//分配每条线程的下载区间 //分配每条线程的下载区间
pro = CommonUtil.loadConfig(mConfigFile); pro = CommonUtil.loadConfig(mConfigFile);
if (pro.isEmpty()) { if (pro.isEmpty()) {
handleNewTask(); handleNewTask(fileLength);
} else { } else {
Set<Object> keys = pro.keySet(); Set<Object> keys = pro.keySet();
int num = 0; int num = 0;
@ -390,7 +386,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
} }
} }
if (num == 0) { if (num == 0) {
handleNewTask(); handleNewTask(fileLength);
return pro; return pro;
} }
THREAD_NUM = num; THREAD_NUM = num;
@ -400,7 +396,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
if (state != null && Integer.parseInt(state + "") == 1) { if (state != null && Integer.parseInt(state + "") == 1) {
continue; continue;
} }
handleNewTask(); handleNewTask(fileLength);
return pro; return pro;
} }
} }
@ -412,9 +408,10 @@ class DownloadUtil implements IDownloadUtil, Runnable {
/** /**
* 处理新任务 * 处理新任务
*/ */
private void handleNewTask() { private void handleNewTask(long fileLength) {
isNewTask = true; isNewTask = true;
THREAD_NUM = AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); THREAD_NUM = fileLength < SUB_LEN ? 1
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum();
} }
/** /**

@ -4,7 +4,7 @@
<!--注意,修改该配置文件中的属性会覆盖代码中所设置的属性--> <!--注意,修改该配置文件中的属性会覆盖代码中所设置的属性-->
<download> <download>
<!--设置下载线程,线程下载数改变后,新的下载任务才会生效--> <!--设置下载线程,线程下载数改变后,新的下载任务才会生效,如果任务大小小于1m,该设置也不会生效-->
<threadNum value="4"/> <threadNum value="4"/>
<!--是否打开下载广播,默认为false,不建议使用广播,你可以使用Download注解来实现事件回调--> <!--是否打开下载广播,默认为false,不建议使用广播,你可以使用Download注解来实现事件回调-->

@ -56,14 +56,17 @@ public class TestMutilTaskSysDownload extends BaseActivity<TestActivityMultiBind
int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum(); int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum();
for (int i = 0; i < urlArray.length; i++) { for (int i = 0; i < urlArray.length; i++) {
if (i < maxNum) { if (i < maxNum) {
Aria.download(this) Aria.download(this)
.load(baseUrl + urlArray[i]) .load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.start(); .start();
} else { } else {
Aria.download(this) Aria.download(this)
.load(baseUrl + urlArray[i]) .load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.add(); .add();
} }
} }

Loading…
Cancel
Save