Commit 226796ec authored by qq247321453's avatar qq247321453

下载文件大小检测

parent c9e97ac8
...@@ -5,6 +5,7 @@ import android.content.Intent; ...@@ -5,6 +5,7 @@ import android.content.Intent;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
...@@ -471,6 +472,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder { ...@@ -471,6 +472,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
@Override @Override
public void onDownloadFailed(Exception e) { public void onDownloadFailed(Exception e) {
Log.w(IrrlichtBridge.TAG, "download image error:" + e.getMessage());
//下载失败后删除下载的文件 //下载失败后删除下载的文件
FileUtils.deleteFile(tmp); FileUtils.deleteFile(tmp);
// downloadCardImage(code, file); // downloadCardImage(code, file);
......
...@@ -44,11 +44,11 @@ public class DownloadUtil { ...@@ -44,11 +44,11 @@ public class DownloadUtil {
*/ */
public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) { public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {
if(ENABLE_CACHE){ if (ENABLE_CACHE) {
synchronized (cache){ synchronized (cache) {
Call old = cache.get(url); Call old = cache.get(url);
if(old != null){ if (old != null) {
Log.w(IrrlichtBridge.TAG, "exist download task by:" +url); Log.w(IrrlichtBridge.TAG, "exist download task by:" + url);
return; return;
} }
} }
...@@ -72,8 +72,8 @@ public class DownloadUtil { ...@@ -72,8 +72,8 @@ public class DownloadUtil {
public void onFailure(Call call, IOException e) { public void onFailure(Call call, IOException e) {
// 下载失败监听回调 // 下载失败监听回调
listener.onDownloadFailed(e); listener.onDownloadFailed(e);
if(ENABLE_CACHE){ if (ENABLE_CACHE) {
synchronized (cache){ synchronized (cache) {
cache.remove(url); cache.remove(url);
} }
} }
...@@ -81,10 +81,16 @@ public class DownloadUtil { ...@@ -81,10 +81,16 @@ public class DownloadUtil {
@Override @Override
public void onResponse(Call call, Response response) throws IOException { public void onResponse(Call call, Response response) throws IOException {
if(!response.isSuccessful()){
listener.onDownloadFailed(new Exception("error:"+response.code()));
return;
}
String contentLen = response.header("Content-Length");
final long contentLength = (contentLen == null || contentLen.length() == 0) ? 0 : Long.parseLong(contentLen);
InputStream is = null; InputStream is = null;
byte[] buf = new byte[2048]; byte[] buf = new byte[2048];
int len = 0; int len = 0;
FileOutputStream fos = null; FileOutputStream out = null;
//储存下载文件的目录 //储存下载文件的目录
File dir = new File(destFileDir); File dir = new File(destFileDir);
...@@ -92,31 +98,36 @@ public class DownloadUtil { ...@@ -92,31 +98,36 @@ public class DownloadUtil {
dir.mkdirs(); dir.mkdirs();
} }
File file = new File(dir, destFileName); File file = new File(dir, destFileName);
boolean saved = false;
try { try {
is = response.body().byteStream(); is = response.body().byteStream();
long total = response.body().contentLength(); long total = response.body().contentLength();
fos = new FileOutputStream(file); out = new FileOutputStream(file);
long sum = 0; long sum = 0;
while ((len = is.read(buf)) != -1) { while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len); out.write(buf, 0, len);
sum += len; sum += len;
int progress = (int) (sum * 1.0f / total * 100); int progress = (int) (sum * 1.0f / total * 100);
//下载中更新进度条 //下载中更新进度条
listener.onDownloading(progress); listener.onDownloading(progress);
} }
fos.flush(); out.flush();
//下载完成 saved = true;
listener.onDownloadSuccess(file);
} catch (Exception ex) { } catch (Exception ex) {
listener.onDownloadFailed(ex); listener.onDownloadFailed(ex);
}finally { } finally {
IOUtils.close(out);
IOUtils.close(is); IOUtils.close(is);
IOUtils.close(fos);
} }
if(ENABLE_CACHE){ if (saved) {
synchronized (cache){ if (contentLength > 0 && file.length() < contentLength) {
listener.onDownloadFailed(new Exception("file length[" + file.length() + "] < " + contentLen));
} else {
listener.onDownloadSuccess(file);
}
}
if (ENABLE_CACHE) {
synchronized (cache) {
cache.remove(url); cache.remove(url);
} }
} }
...@@ -125,7 +136,7 @@ public class DownloadUtil { ...@@ -125,7 +136,7 @@ public class DownloadUtil {
} }
public static interface OnDownloadListener{ public static interface OnDownloadListener {
/** /**
* 下载成功之后的文件 * 下载成功之后的文件
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment