2.apk 安装过程中重启apk已安装未删除安装包修改为删除安装包 3.DownLoadManager撤回下载任务但文件丢失会产生-tmp 0 size大小文件bug,下载异常日志打印
189 lines
7.1 KiB
Java
189 lines
7.1 KiB
Java
package com.android.download;
|
|
|
|
|
|
|
|
import com.android.database.lib.DownLoadTaskBean;
|
|
import com.android.util.LogUtils;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.RandomAccessFile;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
public class DownLoadTaskThread implements Runnable {
|
|
private boolean pause;
|
|
private DownLoadTaskBean bean;
|
|
private long currentTotal = 0;
|
|
private DownloadTaskObserver observer;
|
|
|
|
public DownLoadTaskThread(DownLoadTaskBean bean, DownloadTaskObserver observer) {
|
|
this.bean = bean;
|
|
this.observer = observer;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
HttpURLConnection urlConnection=null;
|
|
RandomAccessFile randomFile=null;
|
|
InputStream inputStream =null;
|
|
try {
|
|
URL url = new URL(bean.getUrl());
|
|
urlConnection = (HttpURLConnection) url.openConnection();
|
|
urlConnection.setConnectTimeout(20000); //设置连接超时事件为20秒
|
|
urlConnection.setRequestMethod("GET"); //设置请求方式为GET
|
|
//设置用户端可以接收的媒体类型
|
|
urlConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, " +
|
|
"image/pjpeg, application/x-shockwave-flash, application/xaml+xml, " +
|
|
"application/vnd.ms-xpsdocument, application/x-ms-xbap," +
|
|
" application/x-ms-application, application/vnd.ms-excel," +
|
|
" application/vnd.ms-powerpoint, application/msword, */*");
|
|
|
|
urlConnection.setRequestProperty("Accept-Language", "zh-CN"); //设置用户语言
|
|
urlConnection.setRequestProperty("Charset", "UTF-8"); //设置客户端编码
|
|
//设置用户代理
|
|
urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; " +
|
|
"Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727;" +
|
|
" .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
|
|
|
|
File dataFile = new File(bean.getPath(), bean.getFileName());
|
|
if(dataFile.exists()&&dataFile.length()==bean.getTotal()){ //判断该文件已下载无需再下载
|
|
LogUtils.loge("dont need download "+bean.getFileName());
|
|
observer.onFinish(bean, bean.getTaskId(), dataFile.length());
|
|
return;
|
|
}
|
|
|
|
//设置文件写入位置
|
|
File file = new File(bean.getPath(), bean.getFileName()+"-tmp");
|
|
if(!file.exists()){
|
|
file.createNewFile();
|
|
}
|
|
currentTotal = file.length();
|
|
LogUtils.loge(bean.getStart() + "start------" + currentTotal + ">>>>" + bean.getTotal());
|
|
if(currentTotal==bean.getTotal()){
|
|
File targFile = new File(bean.getPath(), bean.getFileName());
|
|
file.renameTo(targFile);//重命名
|
|
Thread.sleep(3000);
|
|
observer.onFinish(bean, bean.getTaskId(), currentTotal);
|
|
file.delete();//删除临时文件
|
|
return;
|
|
}
|
|
//设置下载位置
|
|
urlConnection.setRequestProperty("Range", "bytes=" + currentTotal + "-");
|
|
|
|
randomFile = new RandomAccessFile(file, "rwd");
|
|
randomFile.seek(currentTotal);
|
|
urlConnection.connect();
|
|
if (observer != null) {
|
|
observer.onStart(bean, bean.getTaskId());
|
|
}
|
|
|
|
//获得文件流
|
|
inputStream = urlConnection.getInputStream();
|
|
byte[] buffer = new byte[1024*10];
|
|
int len;
|
|
long time = System.currentTimeMillis();
|
|
while ( (currentTotal!=bean.getTotal())&&(len = inputStream.read(buffer)) != -1) {
|
|
|
|
//写入文件
|
|
randomFile.write(buffer, 0, len);
|
|
currentTotal += len;
|
|
//时间间隔大于500ms再发
|
|
if (System.currentTimeMillis() - time > 500) {
|
|
time = System.currentTimeMillis();
|
|
observer.onProgress(bean, bean.getTaskId(), currentTotal);
|
|
}
|
|
//判断是否是暂停状态.保存断点
|
|
if (observer != null && pause) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
urlConnection.disconnect();
|
|
urlConnection=null;
|
|
randomFile.close();
|
|
randomFile=null;
|
|
inputStream.close();
|
|
inputStream=null;
|
|
|
|
observer.onProgress(bean, bean.getTaskId(), currentTotal);
|
|
|
|
if (observer != null && !pause) {
|
|
if(currentTotal == bean.getTotal()) {
|
|
LogUtils.loge("下载完成");
|
|
LogUtils.loge("download Exception===>sleeping before");
|
|
File targFile = new File(bean.getPath(), bean.getFileName());
|
|
file.renameTo(targFile);//重命名
|
|
Thread.sleep(3000);
|
|
LogUtils.loge("download Exception===>sleeping after");
|
|
observer.onFinish(bean, bean.getTaskId(), currentTotal);
|
|
file.delete();//删除临时文件
|
|
}else {
|
|
LogUtils.loge("下载失败");
|
|
if (observer != null) {
|
|
observer.onError(bean, bean.getTaskId(), "下载失败");
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LogUtils.loge("download Exception===>"+e.getMessage());
|
|
if (observer != null) {
|
|
observer.onError(bean, bean.getTaskId(), e.getMessage());
|
|
}
|
|
}finally {
|
|
if(urlConnection!=null) {
|
|
urlConnection.disconnect();
|
|
urlConnection=null;
|
|
}
|
|
try {
|
|
if(randomFile!=null) {
|
|
randomFile.close();
|
|
randomFile=null;
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
try {
|
|
if(inputStream!=null) {
|
|
inputStream.close();
|
|
inputStream=null;
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public DownLoadTaskBean getBean() {
|
|
return bean;
|
|
}
|
|
|
|
public void setBean(DownLoadTaskBean bean) {
|
|
this.bean = bean;
|
|
}
|
|
|
|
public void setPause(boolean pause) {
|
|
this.pause = pause;
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static abstract class DownloadTaskObserver {
|
|
|
|
protected abstract void onStart(DownLoadTaskBean bean, long taskId);
|
|
|
|
protected abstract void onProgress(DownLoadTaskBean bean, long taskId, long progress);
|
|
|
|
protected abstract void onFinish(DownLoadTaskBean bean, long taskId, long lastProgress);
|
|
|
|
protected abstract void onPause(DownLoadTaskBean bean, long taskId, long currentProgress);
|
|
|
|
protected abstract void onError(DownLoadTaskBean bean, long taskId, String erroMsg);
|
|
}
|
|
}
|