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);
......
...@@ -21,7 +21,7 @@ public class DownloadUtil { ...@@ -21,7 +21,7 @@ public class DownloadUtil {
private static DownloadUtil downloadUtil; private static DownloadUtil downloadUtil;
private final OkHttpClient okHttpClient; private final OkHttpClient okHttpClient;
//暂时关闭 //暂时关闭
private static final boolean ENABLE_CACHE = false; private static final boolean ENABLE_CACHE = false;
private static final Map<String, Call> cache = new HashMap<>(); private static final Map<String, Call> cache = new HashMap<>();
public static DownloadUtil get() { public static DownloadUtil get() {
...@@ -44,18 +44,18 @@ public class DownloadUtil { ...@@ -44,18 +44,18 @@ 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;
} }
} }
} }
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.build(); .build();
/* OkHttpClient client = new OkHttpClient(); /* OkHttpClient client = new OkHttpClient();
...@@ -66,66 +66,77 @@ public class DownloadUtil { ...@@ -66,66 +66,77 @@ public class DownloadUtil {
}*/ }*/
//异步请求 //异步请求
Call call = okHttpClient.newCall(request); Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() { call.enqueue(new Callback() {
@Override @Override
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);
} }
} }
} }
@Override @Override
public void onResponse(Call call, Response response) throws IOException { public void onResponse(Call call, Response response) throws IOException {
InputStream is = null; if(!response.isSuccessful()){
byte[] buf = new byte[2048]; listener.onDownloadFailed(new Exception("error:"+response.code()));
int len = 0; return;
FileOutputStream fos = null; }
String contentLen = response.header("Content-Length");
//储存下载文件的目录 final long contentLength = (contentLen == null || contentLen.length() == 0) ? 0 : Long.parseLong(contentLen);
File dir = new File(destFileDir); InputStream is = null;
if (!dir.exists()) { byte[] buf = new byte[2048];
dir.mkdirs(); int len = 0;
} FileOutputStream out = null;
File file = new File(dir, destFileName);
//储存下载文件的目录
try { File dir = new File(destFileDir);
if (!dir.exists()) {
is = response.body().byteStream(); dir.mkdirs();
long total = response.body().contentLength(); }
fos = new FileOutputStream(file); File file = new File(dir, destFileName);
long sum = 0; boolean saved = false;
while ((len = is.read(buf)) != -1) { try {
fos.write(buf, 0, len); is = response.body().byteStream();
sum += len; long total = response.body().contentLength();
int progress = (int) (sum * 1.0f / total * 100); out = new FileOutputStream(file);
//下载中更新进度条 long sum = 0;
listener.onDownloading(progress); while ((len = is.read(buf)) != -1) {
} out.write(buf, 0, len);
fos.flush(); sum += len;
//下载完成 int progress = (int) (sum * 1.0f / total * 100);
//下载中更新进度条
listener.onDownloading(progress);
}
out.flush();
saved = true;
} catch (Exception ex) {
listener.onDownloadFailed(ex);
} finally {
IOUtils.close(out);
IOUtils.close(is);
}
if (saved) {
if (contentLength > 0 && file.length() < contentLength) {
listener.onDownloadFailed(new Exception("file length[" + file.length() + "] < " + contentLen));
} else {
listener.onDownloadSuccess(file); listener.onDownloadSuccess(file);
} catch (Exception ex) {
listener.onDownloadFailed(ex);
}finally {
IOUtils.close(is);
IOUtils.close(fos);
}
if(ENABLE_CACHE){
synchronized (cache){
cache.remove(url);
}
} }
} }
}); if (ENABLE_CACHE) {
synchronized (cache) {
cache.remove(url);
}
}
}
});
} }
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