pull/330/head
laoyuyu 7 years ago
parent 066bbbd006
commit 795b476d6f
  1. 4
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. 31
      Aria/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java
  3. 19
      Aria/src/main/java/com/arialyy/aria/core/common/OnFileInfoCallback.java
  4. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  5. 1
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java
  6. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  7. 5
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  8. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java
  9. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java
  10. 35
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  11. 47
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  12. 5
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  13. 3
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java
  14. 5
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java
  15. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  16. 2
      DEV_LOG.md
  17. 15
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  18. 2
      build.gradle

@ -129,7 +129,11 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
final int num =
mEntity.getFileSize() <= SUB_LEN || mTaskEntity.requestType == AbsTaskEntity.D_FTP_DIR ? 1
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum();
if (mTaskEntity instanceof DownloadTaskEntity && ((DownloadTaskEntity) mTaskEntity).isChunked) {
mStartThreadNum = 1;
} else {
mStartThreadNum = num;
}
return num;
}

@ -0,0 +1,31 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.common;
/**
* Created by AriaL on 2018/3/3.
* 获取文件信息完成后 回调给下载线程的信息
*/
public class CompleteInfo {
/**
* 自定义的状态码
*/
public int code;
public CompleteInfo(int code) {
this.code = code;
}
}

@ -1,12 +1,27 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.common;
public interface OnFileInfoCallback {
/**
* 处理完成
*
* @param code 状态码
* @param info 一些回调的信息
*/
void onComplete(String url, int code);
void onComplete(String url, CompleteInfo info);
/**
* 请求失败

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.inf.AbsNormalTaskEntity;
import com.arialyy.aria.orm.Ignore;
import com.arialyy.aria.orm.NoNull;
import com.arialyy.aria.orm.OneToOne;
@ -37,6 +38,11 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
*/
public String groupName = "";
/**
* 是否是chunk模式
*/
public boolean isChunked = false;
/**
* 该任务是否属于任务组
*/

@ -86,6 +86,7 @@ class ConnectionHelp {
.append("image/jpeg, ")
.append("image/pjpeg, ")
.append("image/webp, ")
.append("image/apng, ")
.append("application/xml, ")
.append("application/xaml+xml, ")
.append("application/xhtml+xml, ")

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download.downloader;
import android.util.SparseArray;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
@ -99,7 +100,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
callback = new OnFileInfoCallback() {
int failNum = 0;
@Override public void onComplete(String url, int code) {
@Override public void onComplete(String url, CompleteInfo info) {
DownloadTaskEntity te = mExeMap.get(url);
if (te != null) {
if (isNeedLoadFileSize) {

@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
@ -41,8 +42,8 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
startDownload();
} else {
new FtpDirInfoThread(mGTEntity, new OnFileInfoCallback() {
@Override public void onComplete(String url, int code) {
if (code >= 200 && code < 300) {
@Override public void onComplete(String url, CompleteInfo info) {
if (info.code >= 200 && info.code < 300) {
for (DownloadEntity entity : mGTEntity.entity.getSubTask()) {
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity));
}

@ -17,6 +17,7 @@ package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.AbsFtpInfoThread;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
@ -48,7 +49,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
@Override protected void onPreComplete(int code) {
super.onPreComplete(code);
mEntity.setFileSize(mSize);
mCallback.onComplete(mEntity.getKey(), code);
mCallback.onComplete(mEntity.getKey(), new CompleteInfo(code));
}
private void addEntity(String remotePath, FTPFile ftpFile) {

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.common.AbsFtpInfoThread;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
@ -40,6 +41,6 @@ class FtpFileInfoThread extends AbsFtpInfoThread<DownloadEntity, DownloadTaskEnt
mTaskEntity.isNewTask = true;
}
mEntity.setFileSize(mSize);
mCallback.onComplete(mEntity.getUrl(), code);
mCallback.onComplete(mEntity.getUrl(), new CompleteInfo(code));
}
}

@ -17,6 +17,7 @@ package com.arialyy.aria.core.download.downloader;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
@ -24,9 +25,12 @@ import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
/**
* 下载文件信息获取
@ -55,6 +59,9 @@ class HttpFileInfoThread implements Runnable {
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
conn.setConnectTimeout(mConnectTimeOut);
conn.setRequestMethod(mTaskEntity.requestEnum.name);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(0);
conn.connect();
handleConnect(conn);
} catch (IOException e) {
@ -84,7 +91,19 @@ class HttpFileInfoThread implements Runnable {
mEntity.setMd5Code(md5Code);
}
String disposition = conn.getHeaderField(mTaskEntity.dispositionKey);
//Map<String, List<String>> headers = conn.getHeaderFields();
Map<String, List<String>> headers = conn.getHeaderFields();
boolean isChunked = false;
//https://my.oschina.net/ososchina/blog/666761
if (conn.getHeaderField("Transfer-Encoding").equals("chunked")) {
isChunked = true;
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[1024];
InputStream is = conn.getInputStream();
int l = 0;
//while (true){
// is.read()
//}
}
if (!TextUtils.isEmpty(disposition)) {
mEntity.setDisposition(CommonUtil.encryptBASE64(disposition));
if (disposition.contains(mTaskEntity.dispositionFileKey)) {
@ -95,12 +114,16 @@ class HttpFileInfoThread implements Runnable {
mTaskEntity.code = code;
if (code == HttpURLConnection.HTTP_PARTIAL) {
if (!checkLen(len)) return;
if (!checkLen(len) && !isChunked) {
return;
}
mEntity.setFileSize(len);
mTaskEntity.isSupportBP = true;
isComplete = true;
} else if (code == HttpURLConnection.HTTP_OK) {
if (!checkLen(len)) return;
if (!checkLen(len) && !isChunked) {
return;
}
mEntity.setFileSize(len);
mTaskEntity.isSupportBP = false;
isComplete = true;
@ -117,10 +140,12 @@ class HttpFileInfoThread implements Runnable {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true);
}
if (isComplete) {
mTaskEntity.isChunked = isChunked;
mTaskEntity.update();
if (onFileInfoListener != null) {
onFileInfoListener.onComplete(mEntity.getUrl(), code);
CompleteInfo info = new CompleteInfo(code);
onFileInfoListener.onComplete(mEntity.getUrl(), info);
}
mTaskEntity.update();
}
}

@ -27,6 +27,8 @@ import com.arialyy.aria.util.CommonUtil;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
@ -71,24 +73,20 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
conn = ConnectionHelp.setConnectParam(mConfig.TASK_ENTITY, conn);
conn.setConnectTimeout(STATE.CONNECT_TIME_OUT);
conn.setReadTimeout(STATE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数
//is = conn.getInputStream();
is = new BufferedInputStream(conn.getInputStream());
//创建可设置位置的文件
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
//设置每条线程写入文件的位置
file.seek(mConfig.START_LOCATION);
byte[] buffer = new byte[mBufSize];
int len;
while ((len = is.read(buffer)) != -1) {
if (STATE.isCancel || STATE.isStop){
break;
}
if (mSleepTime > 0) {
Thread.sleep(mSleepTime);
}
file.write(buffer, 0, len);
progress(len);
if (mTaskEntity.isChunked) {
readChunk(is, file);
} else {
readNormal(is, file);
}
if (STATE.isCancel || STATE.isStop) {
return;
}
@ -137,6 +135,33 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
}
}
/**
* 读取chunk模式的文件流
*/
private void readChunk(InputStream is, RandomAccessFile file)
throws IOException, InterruptedException {
}
/**
* 读取普通的文件流
*/
private void readNormal(InputStream is, RandomAccessFile file)
throws IOException, InterruptedException {
byte[] buffer = new byte[mBufSize];
int len;
while ((len = is.read(buffer)) != -1) {
if (STATE.isCancel || STATE.isStop) {
break;
}
if (mSleepTime > 0) {
Thread.sleep(mSleepTime);
}
file.write(buffer, 0, len);
progress(len);
}
}
@Override protected String getTaskType() {
return "HTTP_DOWNLOAD";
}

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.download.DownloadTaskEntity;
@ -106,7 +107,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
switch (mTaskEntity.requestType) {
case AbsTaskEntity.D_FTP:
return new FtpFileInfoThread(mTaskEntity, new OnFileInfoCallback() {
@Override public void onComplete(String url, int code) {
@Override public void onComplete(String url, CompleteInfo info) {
mDownloader.start();
}
@ -116,7 +117,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
});
case AbsTaskEntity.D_HTTP:
return new HttpFileInfoThread(mTaskEntity, new OnFileInfoCallback() {
@Override public void onComplete(String url, int code) {
@Override public void onComplete(String url, CompleteInfo info) {
mDownloader.start();
}

@ -16,6 +16,7 @@
package com.arialyy.aria.core.upload.uploader;
import com.arialyy.aria.core.common.AbsFtpInfoThread;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTaskEntity;
@ -82,6 +83,6 @@ class FtpFileInfoThread extends AbsFtpInfoThread<UploadEntity, UploadTaskEntity>
@Override protected void onPreComplete(int code) {
super.onPreComplete(code);
mCallback.onComplete(mEntity.getKey(), isComplete ? CODE_COMPLETE : code);
mCallback.onComplete(mEntity.getKey(), new CompleteInfo(isComplete ? CODE_COMPLETE : code));
}
}

@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.upload.uploader;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -51,8 +52,8 @@ public class SimpleUploadUtil implements IUtil, Runnable {
switch (mTaskEntity.requestType) {
case AbsTaskEntity.U_FTP:
new FtpFileInfoThread(mTaskEntity, new OnFileInfoCallback() {
@Override public void onComplete(String url, int code) {
if (code == FtpFileInfoThread.CODE_COMPLETE) {
@Override public void onComplete(String url, CompleteInfo info) {
if (info.code == FtpFileInfoThread.CODE_COMPLETE) {
mListener.onComplete();
} else {
mUploader.start();

@ -34,7 +34,7 @@ import java.util.Map;
class DBConfig {
static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME;
static int VERSION = 23;
static int VERSION = 24;
static {
if (TextUtils.isEmpty(DB_NAME)) {

@ -1,4 +1,6 @@
## 开发日志
+ v_3.3.16
- 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200
+ v_3.3.14
- 修复ftp上传和下载的兼容性问题
- 如果url中的path有"//"将替换为"/"

@ -47,7 +47,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1";
//"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi";
//"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt";
@ -61,6 +61,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://qudao.5535.cn/one/game.html?game=531&cpsuser=xiaoeryu2";
//"https://bogoe-res.mytbz.com/tbzengsong/If You're Happy.mp3";
//"http://ozr0ucjs5.bkt.clouddn.com/51_box-104_20180131202610.apk";
"http://kf.lyfz.net/api/v1/we_chat/Business/getMaterial?company_id=51454009d703c86c91353f61011ecf2f&appid=wx88c6052d06eaaf7d&media_id=qpkQb72KqNkZhBNIUw7asF0LuJouXCTBd3A-rS2bYl7H4kvTZstEXD3F-pDVeqA-&type=3";
//"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864";
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@ -222,15 +224,12 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void startD() {
//Aria.get(this).setLogLevel(ALog.LOG_CLOSE);
//Aria.download(this).load("aaaa.apk");
Map<String, String> map = new HashMap<>();
map.put("User-Agent",
"Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/KTU84P; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 Mobile MQQBrowser/6.2 TBS/043722 Safari/537.36");
map.put("Cookie",
"BAIDUID=DFC7EF42C60AD1ACF0BA94389AA67F13:FG=1; H_WISE_SIDS=121192_104493_114745_121434_119046_100098_120212_121140_118882_118858_118850_118820_118792_121254_121534_121214_117588_117242_117431_119974_120597_121043_121422_120943_121175_121272_117552_120482_121013_119962_119145_120851_120841_120034_121325_116407_121109_120654_110085_120708; PSINO=7; BDORZ=AE84CDB3A529C0F8A2B9DCDD1D18B695");
Aria.download(SingleTaskActivity.this)
.load(DOWNLOAD_URL)
//.addHeader("groupName", "value")
.addHeaders(map)
//.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("DNT", "1")
//.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0")
//.setRequestMode(RequestEnum.POST)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk")
.resetState()

@ -39,7 +39,7 @@ task clean(type: Delete) {
ext {
userOrg = 'arialyy'
groupId = 'com.arialyy.aria'
publishVersion = '3.3.15'
publishVersion = '3.3.16_dev'
// publishVersion = '1.0.3' //FTP插件
repoName='maven'
desc = 'android 下载框架'

Loading…
Cancel
Save