From a8d8440d81219172c746c08b4555d26a513ad3b2 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sat, 5 May 2018 16:45:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/download/DownloadTarget.java | 5 +-- .../downloader/HttpFileInfoThread.java | 44 +++++++++++++++---- .../arialyy/aria/core/manager/TEManager.java | 1 + DEV_LOG.md | 9 ++-- app/src/main/AndroidManifest.xml | 4 +- .../simple/download/SingleTaskActivity.java | 18 +++++--- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index f9b371a1..cba839aa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -53,7 +53,7 @@ public class DownloadTarget extends BaseNormalTarget * * @param use {@code true} 使用 */ - @Deprecated public DownloadTarget useServerFileName(boolean use) { + public DownloadTarget useServerFileName(boolean use) { mTaskEntity.setUseServerFileName(use); return this; } @@ -65,8 +65,7 @@ public class DownloadTarget extends BaseNormalTarget * @param downloadPath 文件保存路径 * @deprecated {@link #setFilePath(String)} 请使用这个api */ - @Deprecated - public DownloadTarget setDownloadPath(@NonNull String downloadPath) { + @Deprecated public DownloadTarget setDownloadPath(@NonNull String downloadPath) { return setFilePath(downloadPath); } 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 f2ff72b4..63634d0f 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 @@ -25,6 +25,7 @@ import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; @@ -98,21 +99,26 @@ class HttpFileInfoThread implements Runnable { mEntity.setMd5Code(md5Code); } - //Map> headers = conn.getHeaderFields(); boolean isChunked = false; final String str = conn.getHeaderField("Transfer-Encoding"); if (!TextUtils.isEmpty(str) && str.equals("chunked")) { isChunked = true; } + //Map> headers = conn.getHeaderFields(); String disposition = conn.getHeaderField("Content-Disposition"); - if (!TextUtils.isEmpty(disposition)) { + if (mTaskEntity.isUseServerFileName() && !TextUtils.isEmpty(disposition)) { mEntity.setDisposition(CommonUtil.encryptBASE64(disposition)); if (disposition.contains(";")) { String[] infos = disposition.split(";"); for (String info : infos) { if (info.startsWith("filename") && info.contains("=")) { String[] temp = info.split("="); - mEntity.setServerFileName(URLDecoder.decode(temp[1], "utf-8")); + if (temp.length > 1) { + String newName = URLDecoder.decode(temp[1], "utf-8"); + mEntity.setServerFileName(newName); + fileRename(newName); + break; + } } } } @@ -136,7 +142,7 @@ class HttpFileInfoThread implements Runnable { sb.append(line); } reader.close(); - handle302Turn(conn, CommonUtil.getWindowReplaceUrl(sb.toString())); + handleUrlReTurn(conn, CommonUtil.getWindowReplaceUrl(sb.toString())); return; } else if (!checkLen(len) && !isChunked) { return; @@ -149,10 +155,7 @@ class HttpFileInfoThread implements Runnable { } else if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_SEE_OTHER) { - mTaskEntity.setRedirectUrl(conn.getHeaderField("Location")); - mEntity.setRedirect(true); - mEntity.setRedirectUrl(mTaskEntity.getRedirectUrl()); - handle302Turn(conn, mTaskEntity.getRedirectUrl()); + handleUrlReTurn(conn, conn.getHeaderField("Location")); } else { failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true); } @@ -166,10 +169,30 @@ class HttpFileInfoThread implements Runnable { } } + /** + * 重命名文件 + */ + private void fileRename(String newName) { + if (TextUtils.isEmpty(newName)) { + ALog.w(TAG, "重命名失败【服务器返回的文件名为空】"); + return; + } + File oldFile = new File(mEntity.getDownloadPath()); + String oldName = oldFile.getName(); + String newPath = oldFile.getParent() + "/" + newName; + if (oldFile.exists()){ + oldFile.renameTo(new File(newPath)); + } + mEntity.setFileName(newName); + mEntity.setDownloadPath(newPath); + mTaskEntity.setKey(newPath); + CommonUtil.renameDownloadConfig(oldName, newName); + } + /** * 处理30x跳转 */ - private void handle302Turn(HttpURLConnection conn, String newUrl) throws IOException { + private void handleUrlReTurn(HttpURLConnection conn, String newUrl) throws IOException { ALog.d(TAG, "30x跳转,新url为【" + newUrl + "】"); if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith( "http")) { @@ -182,6 +205,9 @@ class HttpFileInfoThread implements Runnable { failDownload("下载失败,重定向url错误", false); return; } + mTaskEntity.setRedirectUrl(newUrl); + mEntity.setRedirect(true); + mEntity.setRedirectUrl(newUrl); String cookies = conn.getHeaderField("Set-Cookie"); conn = (HttpURLConnection) new URL(newUrl).openConnection(); conn = ConnectionHelp.setConnectParam(mTaskEntity, conn); diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java index daba832d..1e8ccad1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java @@ -275,6 +275,7 @@ public class TEManager { } private String convertKey(String key) { + key = key.trim(); final Lock lock = this.lock; lock.lock(); try { diff --git a/DEV_LOG.md b/DEV_LOG.md index ef418fb5..961a3125 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,10 +1,11 @@ ## 开发日志 + v_3.4 - 优化大量代码 - - 重构Aria的ORM模型 - - 可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html) - - window.location.replace("http://xxxx")类型的重定向支持 - - 适配gzip、deflate 压缩类型的输入流 + - 重构Aria的ORM模型,提高了数据读取的可靠性和读写速度 + - 现在可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html) + - 添加`window.location.replace("http://xxxx")`类型的网页重定向支持 + - 支持gzip、deflate 压缩类型的输入流 + - 添加`useServerFileName`,可使用服务端响应header的`Content-Disposition`携带的文件名 + v_3.3.16 - 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200 - 优化target代码结构,移除路径被占用的提示 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4696e5dd..bf4f2dc0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,10 +15,10 @@ android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - + 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 ce5f8a8f..f1a9dc69 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -46,8 +46,11 @@ public class SingleTaskActivity extends BaseActivity { //"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://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 - //"https://static.donguo.me//video/ip/course/pfys_1.mp4"; + //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 + //"https://static.donguo.me//video/ip/course/pfys_1.mp4"; + //"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864"; + "https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013"; + @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; @@ -126,7 +129,7 @@ public class SingleTaskActivity extends BaseActivity { @Download.onTaskRunning protected void running(DownloadTask task) { if (task.getKey().equals(DOWNLOAD_URL)) { - Log.d(TAG, task.getKey()); + //Log.d(TAG, task.getKey()); long len = task.getFileSize(); if (len == 0) { getBinding().setProgress(0); @@ -232,12 +235,13 @@ 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("DNT", "1") + //.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) .setRequestMode(RequestEnum.GET) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk") - .resetState() + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg3.apk") + //.resetState() .start(); //.add(); }