添加服务器文件名支持

pull/330/head
laoyuyu 7 years ago
parent 5b645ea494
commit a8d8440d81
  1. 5
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  2. 44
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  3. 1
      Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java
  4. 9
      DEV_LOG.md
  5. 4
      app/src/main/AndroidManifest.xml
  6. 18
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java

@ -53,7 +53,7 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
* *
* @param use {@code true} 使用 * @param use {@code true} 使用
*/ */
@Deprecated public DownloadTarget useServerFileName(boolean use) { public DownloadTarget useServerFileName(boolean use) {
mTaskEntity.setUseServerFileName(use); mTaskEntity.setUseServerFileName(use);
return this; return this;
} }
@ -65,8 +65,7 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
* @param downloadPath 文件保存路径 * @param downloadPath 文件保存路径
* @deprecated {@link #setFilePath(String)} 请使用这个api * @deprecated {@link #setFilePath(String)} 请使用这个api
*/ */
@Deprecated @Deprecated public DownloadTarget setDownloadPath(@NonNull String downloadPath) {
public DownloadTarget setDownloadPath(@NonNull String downloadPath) {
return setFilePath(downloadPath); return setFilePath(downloadPath);
} }

@ -25,6 +25,7 @@ import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -98,21 +99,26 @@ class HttpFileInfoThread implements Runnable {
mEntity.setMd5Code(md5Code); mEntity.setMd5Code(md5Code);
} }
//Map<String, List<String>> headers = conn.getHeaderFields();
boolean isChunked = false; boolean isChunked = false;
final String str = conn.getHeaderField("Transfer-Encoding"); final String str = conn.getHeaderField("Transfer-Encoding");
if (!TextUtils.isEmpty(str) && str.equals("chunked")) { if (!TextUtils.isEmpty(str) && str.equals("chunked")) {
isChunked = true; isChunked = true;
} }
//Map<String, List<String>> headers = conn.getHeaderFields();
String disposition = conn.getHeaderField("Content-Disposition"); String disposition = conn.getHeaderField("Content-Disposition");
if (!TextUtils.isEmpty(disposition)) { if (mTaskEntity.isUseServerFileName() && !TextUtils.isEmpty(disposition)) {
mEntity.setDisposition(CommonUtil.encryptBASE64(disposition)); mEntity.setDisposition(CommonUtil.encryptBASE64(disposition));
if (disposition.contains(";")) { if (disposition.contains(";")) {
String[] infos = disposition.split(";"); String[] infos = disposition.split(";");
for (String info : infos) { for (String info : infos) {
if (info.startsWith("filename") && info.contains("=")) { if (info.startsWith("filename") && info.contains("=")) {
String[] temp = info.split("="); 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); sb.append(line);
} }
reader.close(); reader.close();
handle302Turn(conn, CommonUtil.getWindowReplaceUrl(sb.toString())); handleUrlReTurn(conn, CommonUtil.getWindowReplaceUrl(sb.toString()));
return; return;
} else if (!checkLen(len) && !isChunked) { } else if (!checkLen(len) && !isChunked) {
return; return;
@ -149,10 +155,7 @@ class HttpFileInfoThread implements Runnable {
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP } else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|| code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_SEE_OTHER) { || code == HttpURLConnection.HTTP_SEE_OTHER) {
mTaskEntity.setRedirectUrl(conn.getHeaderField("Location")); handleUrlReTurn(conn, conn.getHeaderField("Location"));
mEntity.setRedirect(true);
mEntity.setRedirectUrl(mTaskEntity.getRedirectUrl());
handle302Turn(conn, mTaskEntity.getRedirectUrl());
} else { } else {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true); 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跳转 * 处理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 + "】"); ALog.d(TAG, "30x跳转,新url为【" + newUrl + "】");
if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith( if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith(
"http")) { "http")) {
@ -182,6 +205,9 @@ class HttpFileInfoThread implements Runnable {
failDownload("下载失败,重定向url错误", false); failDownload("下载失败,重定向url错误", false);
return; return;
} }
mTaskEntity.setRedirectUrl(newUrl);
mEntity.setRedirect(true);
mEntity.setRedirectUrl(newUrl);
String cookies = conn.getHeaderField("Set-Cookie"); String cookies = conn.getHeaderField("Set-Cookie");
conn = (HttpURLConnection) new URL(newUrl).openConnection(); conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn); conn = ConnectionHelp.setConnectParam(mTaskEntity, conn);

@ -275,6 +275,7 @@ public class TEManager {
} }
private String convertKey(String key) { private String convertKey(String key) {
key = key.trim();
final Lock lock = this.lock; final Lock lock = this.lock;
lock.lock(); lock.lock();
try { try {

@ -1,10 +1,11 @@
## 开发日志 ## 开发日志
+ v_3.4 + v_3.4
- 优化大量代码 - 优化大量代码
- 重构Aria的ORM模型 - 重构Aria的ORM模型,提高了数据读取的可靠性和读写速度
- 可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html) - 现在可在任意类中使用Aria了,[使用方法](http://aria.laoyuyu.me/aria_doc/start/any_java.html)
- window.location.replace("http://xxxx")类型的重定向支持 - 添加`window.location.replace("http://xxxx")`类型的网页重定向支持
- 适配gzip、deflate 压缩类型的输入流 - 支持gzip、deflate 压缩类型的输入流
- 添加`useServerFileName`,可使用服务端响应header的`Content-Disposition`携带的文件名
+ v_3.3.16 + v_3.3.16
- 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200 - 修复一个activity启动多次,无法进行回掉的bug https://github.com/AriaLyy/Aria/issues/200
- 优化target代码结构,移除路径被占用的提示 - 优化target代码结构,移除路径被占用的提示

@ -15,10 +15,10 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".test.TestGroupActivity"--> <!--android:name=".test.TestGroupActivity"-->
<!--android:name=".MainActivity"-->
<!--android:name=".test.TestActivity"--> <!--android:name=".test.TestActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<activity <activity
android:name=".test.AnyRunActivity" android:name=".MainActivity"
android:label="@string/app_name"> android:label="@string/app_name">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>

@ -46,8 +46,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; //"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://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
//"https://static.donguo.me//video/ip/course/pfys_1.mp4"; //"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.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel; @Bind(R.id.cancel) Button mCancel;
@ -126,7 +129,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Download.onTaskRunning protected void running(DownloadTask task) { @Download.onTaskRunning protected void running(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) { if (task.getKey().equals(DOWNLOAD_URL)) {
Log.d(TAG, task.getKey()); //Log.d(TAG, task.getKey());
long len = task.getFileSize(); long len = task.getFileSize();
if (len == 0) { if (len == 0) {
getBinding().setProgress(0); getBinding().setProgress(0);
@ -232,12 +235,13 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
Aria.download(SingleTaskActivity.this) Aria.download(SingleTaskActivity.this)
.load(DOWNLOAD_URL) .load(DOWNLOAD_URL)
//.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") //.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("DNT", "1")
//.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0") //.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0")
.useServerFileName(true)
.setRequestMode(RequestEnum.GET) .setRequestMode(RequestEnum.GET)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk") .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg3.apk")
.resetState() //.resetState()
.start(); .start();
//.add(); //.add();
} }

Loading…
Cancel
Save