From 0bf429e36fc5827351f639e8fcdda66b85aa7a33 Mon Sep 17 00:00:00 2001 From: Justson Date: Tue, 6 Mar 2018 22:57:10 +0800 Subject: [PATCH] fix(Downloader.java): fix breakpoint resumes progress calculation error --- .../main/java/com/just/agentweb/download/Downloader.java | 8 ++++++-- .../just/agentweb/sample/fragment/AgentWebFragment.java | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/agentweb-download/src/main/java/com/just/agentweb/download/Downloader.java b/agentweb-download/src/main/java/com/just/agentweb/download/Downloader.java index 48a7d5ae..96d76aba 100644 --- a/agentweb-download/src/main/java/com/just/agentweb/download/Downloader.java +++ b/agentweb-download/src/main/java/com/just/agentweb/download/Downloader.java @@ -230,8 +230,9 @@ private int doDownload() throws IOException { final boolean isEncodingChunked = "chunked".equalsIgnoreCase( mHttpURLConnection.getHeaderField("Transfer-Encoding")); - final boolean hasLength = ((this.mTotals = getHeaderFieldLong(mHttpURLConnection, "Content-Length")) == -1); - LogUtils.i(TAG, "content-length:" + this.mTotals); + long tmpLength = -1; + final boolean hasLength = ((tmpLength = getHeaderFieldLong(mHttpURLConnection, "Content-Length")) == -1); + LogUtils.i(TAG, "content-length:" + this.mTotals + " code:" + mHttpURLConnection.getResponseCode() + " tmpLength:" + tmpLength); // 获取不到文件长度 final boolean finishKnown = isEncodingChunked || hasLength; if (finishKnown) { @@ -250,6 +251,9 @@ private int doDownload() throws IOException { new LoadingRandomAccessFile(mDownloadTask.getFile()), false); case HTTP_PARTIAL: + if (tmpLength + mDownloadTask.getFile().length() != this.mTotals) { + return ERROR_LOAD; + } return transferData(mHttpURLConnection.getInputStream(), new LoadingRandomAccessFile(mDownloadTask.getFile()), true); diff --git a/sample/src/main/java/com/just/agentweb/sample/fragment/AgentWebFragment.java b/sample/src/main/java/com/just/agentweb/sample/fragment/AgentWebFragment.java index 9c1deb37..d3ad7067 100644 --- a/sample/src/main/java/com/just/agentweb/sample/fragment/AgentWebFragment.java +++ b/sample/src/main/java/com/just/agentweb/sample/fragment/AgentWebFragment.java @@ -185,7 +185,7 @@ public boolean onStart(String url, String userAgent, String contentDisposition, .setEnableIndicator(true) // false 关闭进度通知 .addHeader("Cookie", "xx") // 自定义请求头 .setAutoOpen(true) // 下载完成自动打开 - .setForceDownload(false); // 强制下载,不管网络网络类型 + .setForceDownload(true); // 强制下载,不管网络网络类型 return false; } @@ -517,7 +517,7 @@ public boolean onMenuItemClick(MenuItem item) { case R.id.error_website: loadErrorWebSite(); // test DownloadingService - /*LogUtils.i(TAG, " :" + mDownloadingService + " " + (mDownloadingService == null ? "" : mDownloadingService.isShutdown()) + " :" + mExtraService); + /*LogUtils.i(TAG, " :" + mDownloadingService + " " + (mDownloadingService == null ? "" : mDownloadingService.isShutdown()) + " :" + mExtraService); if (mDownloadingService != null && !mDownloadingService.isShutdown()) { mExtraService = mDownloadingService.shutdownNow(); LogUtils.i(TAG, "mExtraService::" + mExtraService);