修复长度小于0,没有停止回调的问题

v3.6.6
laoyuyu 6 years ago
parent a625bd27a3
commit 428edeacd7
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. 1
      Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java
  3. 13
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java
  4. 34
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  6. 14
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  7. 19
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java

@ -205,7 +205,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
}, 0, mUpdateInterval, TimeUnit.MILLISECONDS);
}
protected synchronized void closeTimer() {
public synchronized void closeTimer() {
if (mTimer != null && !mTimer.isShutdown()) {
mTimer.shutdown();
}

@ -111,6 +111,7 @@ public abstract class BaseListener<ENTITY extends AbsEntity, TASK_ENTITY extends
saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress());
handleSpeed(0);
mTask.needRetry = needRetry;
mTask.putExpand(AbsTask.ERROR_INFO_KEY, e);
sendInState2Target(ISchedulers.FAIL);
e.printStackTrace();
ErrorHelp.saveError(e.getTag(), "", ALog.getExceptionString(e));

@ -24,6 +24,9 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.util.SSLContextUtil;
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
@ -154,6 +157,16 @@ class ConnectionHelp {
}
//302获取重定向地址
conn.setInstanceFollowRedirects(false);
CookieManager manager = entity.getCookieManager();
if (manager != null) {
CookieStore store = manager.getCookieStore();
if (store != null && store.getCookies().size() > 0) {
conn.setRequestProperty("Cookie",
TextUtils.join(";", store.getCookies()));
}
}
return conn;
}
}

@ -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<String, List<String>> headers = conn.getHeaderFields();
Map<String, List<String>> 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<String> 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;
}

@ -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();
}
});
}

@ -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<ENTITY extends AbsEntity> extends DbEntity {
*/
public static final int U_FTP = 0xA2;
@Ignore private CookieManager cookieManager;
/**
* 账号和密码
*/
@ -134,6 +140,14 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
public abstract ENTITY getEntity();
public CookieManager getCookieManager() {
return cookieManager;
}
public void setCookieManager(CookieManager cookieManager) {
this.cookieManager = cookieManager;
}
/**
* 获取任务下载状态
*

@ -61,7 +61,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"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<ActivitySingleBinding> {
}
}
@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<ActivitySingleBinding> {
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<ActivitySingleBinding> {
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<ActivitySingleBinding> {
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)

Loading…
Cancel
Save