From 795b476d6f6691e02c25b7fe7a37977ce9701fca Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Mon, 12 Mar 2018 21:51:30 +0800 Subject: [PATCH] chunked --- .../arialyy/aria/core/common/AbsFileer.java | 6 ++- .../aria/core/common/CompleteInfo.java | 31 +++++++++++ .../aria/core/common/OnFileInfoCallback.java | 19 ++++++- .../core/download/DownloadTaskEntity.java | 6 +++ .../download/downloader/ConnectionHelp.java | 1 + .../downloader/DownloadGroupUtil.java | 3 +- .../downloader/FtpDirDownloadUtil.java | 5 +- .../download/downloader/FtpDirInfoThread.java | 3 +- .../downloader/FtpFileInfoThread.java | 3 +- .../downloader/HttpFileInfoThread.java | 35 +++++++++++-- .../download/downloader/HttpThreadTask.java | 51 ++++++++++++++----- .../downloader/SimpleDownloadUtil.java | 5 +- .../upload/uploader/FtpFileInfoThread.java | 3 +- .../upload/uploader/SimpleUploadUtil.java | 5 +- .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- DEV_LOG.md | 2 + .../simple/download/SingleTaskActivity.java | 15 +++--- build.gradle | 2 +- 18 files changed, 156 insertions(+), 41 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index c7fb1145..bf30ff74 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -129,7 +129,11 @@ public abstract class AbsFileer { */ public String groupName = ""; + /** + * 是否是chunk模式 + */ + public boolean isChunked = false; + /** * 该任务是否属于任务组 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index a4a2dfd4..6135ec6f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -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, ") diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index 937f535f..7699077d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -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) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index cba16bd5..719785c2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -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)); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java index caec83c6..8b4bf128 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java @@ -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> headers = conn.getHeaderFields(); + Map> 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(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 8b6f09c5..eadf2132 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -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,25 +73,21 @@ final class HttpThreadTask extends AbsThreadTask 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){ + + if (STATE.isCancel || STATE.isStop) { return; } //支持断点的处理 @@ -105,7 +103,7 @@ final class HttpThreadTask extends AbsThreadTask 0) { + Thread.sleep(mSleepTime); + } + file.write(buffer, 0, len); + progress(len); + } + } + @Override protected String getTaskType() { return "HTTP_DOWNLOAD"; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java index ee99b3a5..3d2dc31b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java @@ -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(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java index 987330d2..f31ae371 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java @@ -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 @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)); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java index 94c9d486..423237a5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java @@ -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(); diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 5196af1b..877223ea 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -34,7 +34,7 @@ import java.util.Map; class DBConfig { static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 23; + static int VERSION = 24; static { if (TextUtils.isEmpty(DB_NAME)) { diff --git a/DEV_LOG.md b/DEV_LOG.md index 8caf37f3..b032d00e 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,4 +1,6 @@ ## 开发日志 + + v_3.3.16 + - 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200 + v_3.3.14 - 修复ftp上传和下载的兼容性问题 - 如果url中的path有"//"将替换为"/" diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 58a781ce..96fc98ac 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -47,7 +47,7 @@ public class SingleTaskActivity extends BaseActivity { 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 { //"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 { private void startD() { //Aria.get(this).setLogLevel(ALog.LOG_CLOSE); //Aria.download(this).load("aaaa.apk"); - Map 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() diff --git a/build.gradle b/build.gradle index c525f86b..9d7946bb 100644 --- a/build.gradle +++ b/build.gradle @@ -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 下载框架'