断点续传
1. 原理:新建数据库帮助类dbhelper
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "download.db", null, 1);
}
/**
* 数据库下载表download_info
* 开始节点,结束节点,文件的总大小,访问的网址
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer, "
+ "start_pos integer, end_pos integer, compelete_size integer,url char)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
package com.example.test006.entity;
public class DownloadInfo {
private int threadId;// 下载器id
private int startPos;// 开始点
private int endPos;// 结束点
private int compeleteSize;// 完成度
private String url;// 下载器网络标识
public DownloadInfo(int threadId, int startPos, int endPos,
int compeleteSize, String url) {
super();
this.threadId = threadId;
this.startPos = startPos;
this.endPos = endPos;
this.compeleteSize = compeleteSize;
this.url = url;
}
public int getThreadId() {
return threadId;
}
public void setThreadId(int threadId) {
this.threadId = threadId;
}
public int getStartPos() {
return startPos;
}
public void setStartPos(int startPos) {
this.startPos = startPos;
}
public int getEndPos() {
return endPos;
}
public void setEndPos(int endPos) {
this.endPos = endPos;
}
public int getCompeleteSize() {
return compeleteSize;
}
public void setCompeleteSize(int compeleteSize) {
this.compeleteSize = compeleteSize;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "DownloadInfo [threadId=" + threadId
+ ", startPos=" + startPos + ", endPos=" + endPos
+ ", compeleteSize=" + compeleteSize +"]";
}
}
package com.example.test006.entity;
public class LoadInfo {
public int fileSize;// 文件大小
private int complete;// 完成度
private String urlstring;// 下载器标识
public LoadInfo() {
super();
// TODO Auto-generated constructor stub
}
public LoadInfo(int fileSize, int complete, String urlstring) {
super();
this.fileSize = fileSize;
this.complete = complete;
this.urlstring = urlstring;
}
public int getFileSize() {
return fileSize;
}
public void setFileSize(int fileSize) {
this.fileSize = fileSize;
}
public int getComplete() {
return complete;
}
public void setComplete(int complete) {
this.complete = complete;
}
public String getUrlstring() {
return urlstring;
}
public void setUrlstring(String urlstring) {
this.urlstring = urlstring;
}
@Override
public String toString() {
return "LoadInfo [fileSize=" + fileSize + ", complete=" + complete
+ ", urlstring=" + urlstring + "]";
}
}
4.dao层,与数据库直接交互
/**
*
* 一个业务类
*/
public class Dao {
private DBHelper dbHelper;
public Dao(Context context) {
dbHelper = new DBHelper(context);
}
/**
* 查看数据库中是否有数据
*/
public boolean isHasInfors(String urlstr) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
String sql = "select count(*) from download_info where url=?";
Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
cursor.moveToFirst();
int count = cursor.getInt(0);
cursor.close();
return count == 0;
}
/**
* 保存 下载的具体信息
*/
public void saveInfos(List infos) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
for (DownloadInfo info : infos) {
String sql = "insert into download_info(thread_id,start_pos, end_pos,compelete_size,url) values (?,?,?,?,?)";
Object[] bindArgs = { info.getThreadId(), info.getStartPos(),
info.getEndPos(), info.getCompeleteSize(), info.getUrl() };
database.execSQL(sql, bindArgs);
}
}
/**
* 得到下载具体信息
*/
public List getInfos(String urlstr) {
List list = new ArrayList();
SQLiteDatabase database = dbHelper.getReadableDatabase();
String sql = "select thread_id, start_pos, end_pos,compelete_size,url from download_info where url=?";
Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
while (cursor.moveToNext()) {
DownloadInfo info = new DownloadInfo(cursor.getInt(0),
cursor.getInt(1), cursor.getInt(2), cursor.getInt(3),
cursor.getString(4));
list.add(info);
}
cursor.close();
return list;
}
/**
* 更新数据库中的下载信息
*/
public void updataInfos(int threadId, int compeleteSize, String urlstr) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
String sql = "update download_info set compelete_size=? where thread_id=? and url=?";
Object[] bindArgs = { compeleteSize, threadId, urlstr };
database.execSQL(sql, bindArgs);
}
/**
* 关闭数据库
*/
public void closeDb() {
dbHelper.close();
}
/**
* 下载完成后删除数据库中的数据
*/
public void delete(String url) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
database.delete("download_info", "url=?", new String[] { url });
database.close();
}
}
public class DownLoader {
private String urlstr;// 下载的地址
private String localfile;// 保存路径
private int threadcount;// 线程数
private Handler mHandler;// 消息处理器
private Dao dao;// 工具类
private int fileSize;// 所要下载的文件的大小
private List infos;// 存放下载信息类的集合
private static final int INIT = 1;// 定义三种下载的状态:初始化状态,正在下载状态,暂停状态
private static final int DOWNLOADING = 2;
private static final int PAUSE = 3;
private int state = INIT;
public DownLoader(String urlstr, String localfile, int threadcount,
Context context, Handler mHandler) {
this.urlstr = urlstr;
this.localfile = localfile;
this.threadcount = threadcount;
this.mHandler = mHandler;
dao = new Dao(context);
}
/**
* 判断是否正在下载
*/
public boolean isdownloading() {
return state == DOWNLOADING;
}
/**
* 得到downloader里的信息 首先进行判断是否是第一次下载,如果是第一次就要进行初始化,并将下载器的信息保存到数据库中
* 如果不是第一次下载,那就要从数据库中读出之前下载的信息(起始位置,结束为止,文件大小等),并将下载信息返回给下载器
*/
public LoadInfo getDownloaderInfors() {
if (isFirst(urlstr)) {
init();
int range = fileSize / threadcount;
infos = new ArrayList();
for (int i = 0; i < threadcount - 1; i++) {
DownloadInfo info = new DownloadInfo(i, i * range, (i + 1)
* range - 1, 0, urlstr);
infos.add(info);
}
DownloadInfo info = new DownloadInfo(threadcount - 1,
(threadcount - 1) * range, fileSize - 1, 0, urlstr);
infos.add(info);
// 保存infos中的数据到数据库
dao.saveInfos(infos);
// 创建一个LoadInfo对象记载下载器的具体信息
LoadInfo loadInfo = new LoadInfo(fileSize, 0, urlstr);
return loadInfo;
} else {
// 得到数据库中已有的urlstr的下载器的具体信息
infos = dao.getInfos(urlstr);
Log.v("TAG", "not isFirst size=" + infos.size());
int size = 0;
int compeleteSize = 0;
for (DownloadInfo info : infos) {
//
compeleteSize += info.getCompeleteSize();
//剩下的总大小
size += info.getEndPos() - info.getStartPos() + 1;
}
return new LoadInfo(size, compeleteSize, urlstr);
}
}
/**
* 初始化网络
*/
private void init() {
try {
URL url = new URL(urlstr);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
fileSize = connection.getContentLength();
File file = new File(localfile);
if (!file.exists()) {
file.createNewFile();
}
// 本地访问文件
RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
accessFile.setLength(fileSize);
accessFile.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判断是否是第一次 下载
*/
private boolean isFirst(String urlstr) {
return dao.isHasInfors(urlstr);
}
/**
* 114 * 利用线程开始下载数据 115
*/
public void download() {
if (infos != null) {
if (state == DOWNLOADING)
return;
state = DOWNLOADING;
for (DownloadInfo info : infos) {
new MyThread(info.getThreadId(), info.getStartPos(),
info.getEndPos(), info.getCompeleteSize(),
info.getUrl()).start();
}
}
}
public class MyThread extends Thread {
private int threadId;
private int startPos;
private int endPos;
private int compeleteSize;
private String urlstr;
public MyThread(int threadId, int startPos, int endPos,
int compeleteSize, String urlstr) {
this.threadId = threadId;
this.startPos = startPos;
this.endPos = endPos;
this.compeleteSize = compeleteSize;
this.urlstr = urlstr;
}
@Override
public void run() {
HttpURLConnection connection = null;
RandomAccessFile randomAccessFile = null;
InputStream is = null;
try {
URL url = new URL(urlstr);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
// 设置范围,格式为Range:bytes x-y;
connection.setRequestProperty("Range", "bytes="
+ (startPos + compeleteSize) + "-" + endPos);
//跳过前边的所有字节数
randomAccessFile = new RandomAccessFile(localfile, "rwd");
randomAccessFile.seek(startPos + compeleteSize);
// 将要下载的文件写到保存在保存路径下的文件中
is = connection.getInputStream();
byte[] buffer = new byte[4096];
int length = -1;
while ((length = is.read(buffer)) != -1) {
randomAccessFile.write(buffer, 0, length);
compeleteSize += length;
// 更新数据库中的下载信息
dao.updataInfos(threadId, compeleteSize, urlstr);
// 用消息将下载信息传给进度条,对进度条进行更新
Message message = Message.obtain();
message.what = 1;
message.obj = urlstr;
message.arg1 = length;
mHandler.sendMessage(message);
if (state == PAUSE) {
return;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
randomAccessFile.close();
connection.disconnect();
dao.closeDb();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 删除数据库中urlstr对应的下载器信息
public void delete(String urlstr) {
dao.delete(urlstr);
}
// 设置暂停
public void pause() {
state = PAUSE;
}
// 重置下载状态
public void reset() {
state = INIT;
}
}
public class MainActivity extends ListActivity {
// 固定下载的资源路径,这里可以设置网络上的地址
private static final String URL = "http://10.0.3.2:8080/TomcatTest/xiangce/";
// 固定存放下载的音乐的路径:SD卡目录下
private static final String SD_PATH = "/mnt/sdcard/";
// 存放各个下载器
private Map downloaders = new HashMap();
// 存放与下载器对应的进度条
private Map ProgressBars = new HashMap();
/**
* 31 * 利用消息处理机制适时更新进度条 32
*/
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
String url = (String) msg.obj;
int length = msg.arg1;
ProgressBar bar = ProgressBars.get(url);
if (bar != null) {
// 设置进度条按读取的length长度更新
bar.incrementProgressBy(length);
if (bar.getProgress() == bar.getMax()) {
Toast.makeText(MainActivity.this, "下载完成!", 0).show();
// 下载完成后清除进度条并将map中的数据清空
LinearLayout layout = (LinearLayout) bar.getParent();
layout.removeView(bar);
ProgressBars.remove(url);
downloaders.get(url).delete(url);
downloaders.get(url).reset();
downloaders.remove(url);
}
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showListView();
}
// 显示listView,这里可以随便添加音乐
private void showListView() {
List