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

@ -81,7 +81,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
+ ".properties");
try {
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
handleNewTask();
isNewTask = true;
CommonUtil.createFile(mConfigFile.getPath());
} else {
isNewTask = !mDownloadFile.exists();
@ -295,10 +295,6 @@ class DownloadUtil implements IDownloadUtil, Runnable {
return;
}
int fileLength = conn.getContentLength();
if (fileLength < SUB_LEN) {
THREAD_NUM = 1;
CONSTANCE.THREAD_NUM = THREAD_NUM;
}
Properties pro = createConfigFile(fileLength);
int blockSize = fileLength / THREAD_NUM;
int[] recordL = new int[THREAD_NUM];
@ -325,7 +321,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
recordL[rl] = i;
rl++;
} else {
handleNewTask();
handleNewTask(fileLength);
}
if (isNewTask) {
recordL[rl] = i;
@ -380,7 +376,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
//分配每条线程的下载区间
pro = CommonUtil.loadConfig(mConfigFile);
if (pro.isEmpty()) {
handleNewTask();
handleNewTask(fileLength);
} else {
Set<Object> keys = pro.keySet();
int num = 0;
@ -390,7 +386,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
}
}
if (num == 0) {
handleNewTask();
handleNewTask(fileLength);
return pro;
}
THREAD_NUM = num;
@ -400,7 +396,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
if (state != null && Integer.parseInt(state + "") == 1) {
continue;
}
handleNewTask();
handleNewTask(fileLength);
return pro;
}
}
@ -412,9 +408,10 @@ class DownloadUtil implements IDownloadUtil, Runnable {
/**
* 处理新任务
*/
private void handleNewTask() {
private void handleNewTask(long fileLength) {
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>
<!--设置下载线程,线程下载数改变后,新的下载任务才会生效-->
<!--设置下载线程,线程下载数改变后,新的下载任务才会生效,如果任务大小小于1m,该设置也不会生效-->
<threadNum value="4"/>
<!--是否打开下载广播,默认为false,不建议使用广播,你可以使用Download注解来实现事件回调-->

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

Loading…
Cancel
Save