Commit 226796ec authored by qq247321453's avatar qq247321453

下载文件大小检测

parent c9e97ac8
......@@ -5,6 +5,7 @@ import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
......@@ -471,6 +472,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
@Override
public void onDownloadFailed(Exception e) {
Log.w(IrrlichtBridge.TAG, "download image error:" + e.getMessage());
//下载失败后删除下载的文件
FileUtils.deleteFile(tmp);
// downloadCardImage(code, file);
......
......@@ -44,11 +44,11 @@ public class DownloadUtil {
*/
public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {
if(ENABLE_CACHE){
synchronized (cache){
if (ENABLE_CACHE) {
synchronized (cache) {
Call old = cache.get(url);
if(old != null){
Log.w(IrrlichtBridge.TAG, "exist download task by:" +url);
if (old != null) {
Log.w(IrrlichtBridge.TAG, "exist download task by:" + url);
return;
}
}
......@@ -72,8 +72,8 @@ public class DownloadUtil {
public void onFailure(Call call, IOException e) {
// 下载失败监听回调
listener.onDownloadFailed(e);
if(ENABLE_CACHE){
synchronized (cache){
if (ENABLE_CACHE) {
synchronized (cache) {
cache.remove(url);
}
}
......@@ -81,10 +81,16 @@ public class DownloadUtil {
@Override
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;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
FileOutputStream out = null;
//储存下载文件的目录
File dir = new File(destFileDir);
......@@ -92,31 +98,36 @@ public class DownloadUtil {
dir.mkdirs();
}
File file = new File(dir, destFileName);
boolean saved = false;
try {
is = response.body().byteStream();
long total = response.body().contentLength();
fos = new FileOutputStream(file);
out = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
out.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
//下载中更新进度条
listener.onDownloading(progress);
}
fos.flush();
//下载完成
listener.onDownloadSuccess(file);
out.flush();
saved = true;
} catch (Exception ex) {
listener.onDownloadFailed(ex);
}finally {
} finally {
IOUtils.close(out);
IOUtils.close(is);
IOUtils.close(fos);
}
if(ENABLE_CACHE){
synchronized (cache){
if (saved) {
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);
}
}
......@@ -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