全志Launcher代码首次提交
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
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()){ //判断该文件已下载无需再下载
|
||||
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);//重命名
|
||||
file.delete();//删除临时文件
|
||||
Thread.sleep(5000);
|
||||
observer.onFinish(bean, bean.getTaskId(), currentTotal);
|
||||
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);//重命名
|
||||
file.delete();//删除临时文件
|
||||
Thread.sleep(5000);
|
||||
LogUtils.loge("download Exception===>sleeping after");
|
||||
observer.onFinish(bean, bean.getTaskId(), currentTotal);
|
||||
}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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user