From 428edeacd791299dbc7dd1cd27ac9bd36ab2f4e0 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Tue, 20 Nov 2018 20:18:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=95=BF=E5=BA=A6=E5=B0=8F?= =?UTF-8?q?=E4=BA=8E0=EF=BC=8C=E6=B2=A1=E6=9C=89=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/aria/core/common/AbsFileer.java | 2 +- .../aria/core/common/BaseListener.java | 1 + .../download/downloader/ConnectionHelp.java | 13 +++++++ .../downloader/HttpFileInfoThread.java | 34 +++++++++++++++---- .../downloader/SimpleDownloadUtil.java | 2 ++ .../arialyy/aria/core/inf/AbsTaskEntity.java | 14 ++++++++ .../simple/download/SingleTaskActivity.java | 19 ++++++----- 7 files changed, 68 insertions(+), 17 deletions(-) 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 f6b6b3b7..33f8cf56 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 @@ -205,7 +205,7 @@ public abstract class AbsFileer 0) { + conn.setRequestProperty("Cookie", + TextUtils.join(";", store.getCookies())); + } + } + return conn; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index 049937aa..3cd5e123 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download.downloader; import android.os.Process; import android.text.TextUtils; +import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.OnFileInfoCallback; @@ -34,10 +35,15 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.HttpCookie; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Set; @@ -136,7 +142,7 @@ class HttpFileInfoThread implements Runnable { if (!TextUtils.isEmpty(str) && str.equals("chunked")) { isChunked = true; } - //Map> headers = conn.getHeaderFields(); + Map> headers = conn.getHeaderFields(); String disposition = conn.getHeaderField("Content-Disposition"); if (mTaskEntity.isUseServerFileName() && !TextUtils.isEmpty(disposition)) { mEntity.setDisposition(CommonUtil.encryptBASE64(disposition)); @@ -155,10 +161,24 @@ class HttpFileInfoThread implements Runnable { } } } + CookieManager msCookieManager = new CookieManager(); + List cookiesHeader = headers.get("Set-Cookie"); + + if (cookiesHeader != null) { + for (String cookie : cookiesHeader) { + msCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); + } + mTaskEntity.setCookieManager(msCookieManager); + } mTaskEntity.setCode(code); if (code == HttpURLConnection.HTTP_PARTIAL) { if (!checkLen(len) && !isChunked) { + if (len < 0) { + failDownload( + new AriaIOException(TAG, String.format("任务下载失败,文件长度小于0, url: %s", mEntity.getUrl())), + false); + } return; } mEntity.setFileSize(len); @@ -181,6 +201,12 @@ class HttpFileInfoThread implements Runnable { handleUrlReTurn(conn, CommonUtil.getWindowReplaceUrl(sb.toString())); return; } else if (!checkLen(len) && !isChunked) { + if (len < 0) { + failDownload( + new AriaIOException(TAG, String.format("任务下载失败,文件长度小于0, url: %s", mEntity.getUrl())), + false); + } + ALog.d(TAG, "len < 0"); return; } mEntity.setFileSize(len); @@ -270,12 +296,6 @@ class HttpFileInfoThread implements Runnable { if (len != mEntity.getFileSize()) { mTaskEntity.setNewTask(true); } - if (len < 0) { - failDownload( - new AriaIOException(TAG, String.format("任务下载失败,文件长度小于0, url: %s", mEntity.getUrl())), - true); - return false; - } return true; } 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 9c6c0304..5015b211 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 @@ -127,6 +127,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable { @Override public void onFail(String url, BaseException e, boolean needRetry) { failDownload(e, needRetry); + mDownloader.closeTimer(); } }); case AbsTaskEntity.D_HTTP: @@ -137,6 +138,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable { @Override public void onFail(String url, BaseException e, boolean needRetry) { failDownload(e, needRetry); + mDownloader.closeTimer(); } }); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index 3e7331aa..74616c0a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -19,8 +19,12 @@ import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.annotation.Ignore; +import java.net.CookieManager; +import java.net.HttpCookie; import java.net.Proxy; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -55,6 +59,8 @@ public abstract class AbsTaskEntity extends DbEntity { */ public static final int U_FTP = 0xA2; + @Ignore private CookieManager cookieManager; + /** * 账号和密码 */ @@ -134,6 +140,14 @@ public abstract class AbsTaskEntity extends DbEntity { public abstract ENTITY getEntity(); + public CookieManager getCookieManager() { + return cookieManager; + } + + public void setCookieManager(CookieManager cookieManager) { + this.cookieManager = cookieManager; + } + /** * 获取任务下载状态 * 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 8a4c583a..deb06a53 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -61,7 +61,7 @@ public class SingleTaskActivity extends BaseActivity { //"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1"; //"https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; //"http://9.9.9.59:5000/download/CentOS-7-x86_64-Minimal-1804.iso"; - "http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zip"; + "https://firmwareapi.azurewebsites.net/firmware-overview?name=A19_Filament_W_IMG0038_00102411-encrypted.ota"; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; @@ -178,10 +178,11 @@ public class SingleTaskActivity extends BaseActivity { } } - @Download.onTaskFail void taskFail(DownloadTask task) { + @Download.onTaskFail void taskFail(DownloadTask task, Exception e) { if (task.getKey().equals(DOWNLOAD_URL)) { Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); setBtState(true); + //ALog.d(TAG, ALog.getExceptionString(e)); } } @@ -196,11 +197,11 @@ public class SingleTaskActivity extends BaseActivity { L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath()); L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); L.d(TAG, "data ==> " + Aria.download(this).getDownloadEntity(DOWNLOAD_URL)); - //Intent install = new Intent(Intent.ACTION_VIEW); - //install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - //File apkFile = new File(task.getDownloadPath()); - //install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); - //startActivity(install); + Intent install = new Intent(Intent.ACTION_VIEW); + install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + File apkFile = new File(task.getDownloadPath()); + install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); + startActivity(install); } } @@ -251,7 +252,7 @@ public class SingleTaskActivity extends BaseActivity { private void startD() { //Aria.get(this).setLogLevel(ALog.LOG_CLOSE); //Aria.download(this).load("aaaa.apk"); - String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg11.apk"; + String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg11.ota"; //File file = new File(path); //if (file.exists()){ // file.delete(); @@ -260,7 +261,7 @@ public class SingleTaskActivity extends BaseActivity { Aria.download(SingleTaskActivity.this) .load(DOWNLOAD_URL) //.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("Accept-Encoding", "gzip, deflate") //.addHeader("DNT", "1") //.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0") .useServerFileName(true)