public class FileInfo implements Serializable{
private String url; //URL
private int length; //长度或结束位置
private int start; //开始位置
private int now;//当前进度
//构造方法,set/get略
}
//开始按钮逻辑,停止逻辑大致相同
strat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,DownLoadService.class);
intent.setAction(DownLoadService.ACTION_START);
intent.putExtra("fileUrl",info);
startService(intent);
}
});
public void run() {
HttpURLConnection urlConnection = null;
RandomAccessFile randomFile = null;
try {
URL url = new URL(fileInfo.getUrl());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setRequestMethod("GET");
int length = -1;
if (urlConnection.getResponseCode() == HttpStatus.SC_OK) {
//获得文件长度
length = urlConnection.getContentLength();
}
if (length <= 0) {
return;
}
//创建相同大小的本地文件
File dir = new File(DOWNLOAD_PATH);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, FILE_NAME);
randomFile = new RandomAccessFile(file, "rwd");
randomFile.setLength(length);
//长度给fileInfo对象
fileInfo.setLength(length);
//通过Handler将对象传递给Service
mHandle.obtainMessage(0, fileInfo).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
} finally { //流的回收逻辑略
}
}
}
downLoadUtil = new DownLoadUtil(DownLoadService.this,info); downLoadUtil.download();
public interface ThreadDAO {
//插入一条数据
public void insert(FileInfo info);
//根据URL删除一条数据
public void delete(String url);
//根据URL更新一条进度
public void update(String url,int finished);
//根据URL找到一条数据
public List<FileInfo> get(String url);
//是否存在
public boolean isExits(String url);
}
public class DownLoadUtil {
//构造方法略
public void download(){
List<FileInfo> lists = threadDAO.get(fileInfo.getUrl());
FileInfo info = null;
if(lists.size() == 0){
//第一次下载,创建子线程下载
new MyThread(fileInfo).start();
}else{
//中间开始的
info = lists.get(0);
new MyThread(info).start();
}
}
class MyThread extends Thread{
private FileInfo info = null;
public MyThread(FileInfo threadInfo) {
this.info = threadInfo;
}
@Override
public void run() {
//向数据库添加线程信息
if(!threadDAO.isExits(info.getUrl())){
threadDAO.insert(info);
}
HttpURLConnection urlConnection = null;
RandomAccessFile randomFile =null;
InputStream inputStream = null;
try {
URL url = new URL(info.getUrl());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setRequestMethod("GET");
//设置下载位置
int start = info.getStart() + info.getNow();
urlConnection.setRequestProperty("Range","bytes=" + start + "-" + info.getLength());
//设置文件写入位置
File file = new File(DOWNLOAD_PATH,FILE_NAME);
randomFile = new RandomAccessFile(file, "rwd");
randomFile.seek(start);
//向Activity发广播
Intent intent = new Intent(ACTION_UPDATE);
finished += info.getNow();
if (urlConnection.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) {
//获得文件流
inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[512];
int len = -1;
long time = System.currentTimeMillis();
while ((len = inputStream.read(buffer))!= -1){
//写入文件
randomFile.write(buffer,0,len);
//把进度发送给Activity
finished += len;
//看时间间隔,时间间隔大于500ms再发
if(System.currentTimeMillis() - time >500){
time = System.currentTimeMillis();
intent.putExtra("now",finished *100 /fileInfo.getLength());
context.sendBroadcast(intent);
}
//判断是否是暂停状态
if(isPause){
threadDAO.update(info.getUrl(),finished);
return; //结束循环
}
}
//删除线程信息
threadDAO.delete(info.getUrl());
}
}catch (Exception e){
e.printStackTrace();
}finally {//回收工作略
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有