最近看了一些大佬去面试的时候都提到了断点续传,所以自己也写一个记录下来,断点续传的原理就是通过数据库实时的去保存当前下载的长度,然后下次再次下载的时候通过setRequestProperty告诉服务端我需要这个文件从什么地方开始下载,我们再通过RandomAccessFile去设置开始写入的位置
效果
首先上效果图与日志,GIF只截取了5秒
步骤
- 首先我们建立一个实体类,用于保存文件的下载进度信息
public class FileInfo implements Serializable{
private String url;
private long length;//文件的总长度,去服务端获取
private int start;//开始的下载位置
private int now;//当前的位置
.....get set方法省略........
}
- 建立一个数据库,用于保存FileInfo,需要增删查改四个方法
- 创建一个服务,在onStartCommand中去根据URL去查找是否有匹配的数据信息,如果没有,俺就是第一次下载,如果有,那就是断点续传
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(DownConstans.DOWNLOAD_START)) {
isPause = false;
//开始下载,启动线程
String url=intent.getStringExtra("url");
FileInfo fileInfo = new FileInfo();
fileInfo.setUrl(url);
downLoadDao = new DownLoadDAO(this);
//从数据库中根据URL去查找是否有和该URL匹配的信息
List<FileInfo> infos = downLoadDao.get(url);
if (infos.size() == 0) {
//第一次下载
Log.e(TAG, "onHandleIntent: 第一次下载" );
downLoadDao.insert(fileInfo);
down(fileInfo);
new DownLoadThread(fileInfo).start();
} else {
//断点续传
Log.e(TAG, "onHandleIntent: 断点续传" );
new DownLoadThread(infos.get(0)).start();
}
} else if (intent.getAction().equals(DownConstans.DOWNLOAD_STOP)) {
//下载标示设置为暂停
isPause=true;
}
return super.onStartCommand(intent, flags, startId);
}
-
在线程中执行下载操作,核心的几个操作:请求、写入
- 请求,通过connection.setRequestProperty("Range","bytes=" + info.getNow() + "-");对我们要读去的字节进行控制,这里有一步需要注意,获取要下载文件的长度要放在setRequestProperty之后进行
1.Range=0-100代表只读取前100个字节。 2.Range=100-500代表读取从第100个字节开始,读到第500个字节为止。 3.Range=100-则代表从第100个字节开始读取,一直读取到文件末尾结束。
- 写入,由于普通的File无法去设置定点写入,所以这里需要用到 RandomAccessFile的seek()方法,去设置我当前要写入的位置,如果当前选择了暂停,则我们将下载的进度保存在数据库中,然后退出下载方法,如果全部下载完了,那么就在数据库中删除掉该条url的数据
这里在下载之前,我们还需要判断之前下载的文件是否已经被删除了,如果删除了,那我们就不能是断点续传了,需要从头下起了
class DownLoadThread extends Thread{
private FileInfo info;
public DownLoadThread(FileInfo info) {
this.info = info;
}
@Override
public void run() {
HttpURLConnection connection = null;
RandomAccessFile randomFile = null;
InputStream inputStream = null;
int now=0;
try {
URL url = new URL(info.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.setRequestProperty("Range","bytes=" + info.getNow() + "-");//设置当前位置,第一次下载时当前位置为0,断点续传时当前位置为上次下载暂停的位置
int contentLength = connection.getContentLength();
info.setLength(contentLength);
if (contentLength <= 0) {
return;
}
File file = new File(DownConstans.DOWNLOAD_PATH,"12345.jpeg");
randomFile = new RandomAccessFile(file, "rwd");
randomFile.seek(info.getNow());
//向Activity发送广播
Intent intent = new Intent(DownConstans.DOWNLOAD_UPDATE);
Log.e(TAG, "now: "+info.getNow());
now = +info.getNow();
if (connection.getResponseCode() == 206) {
//获得文件流
inputStream = connection.getInputStream();
byte[] buffer = new byte[512];
int len = -1;
while ((len = inputStream.read(buffer))!= -1){
//写入文件
randomFile.write(buffer,0,len);
//把进度发送给Activity
now += len;
int progress = (int) (now * 100 / info.getLength());
intent.putExtra("now",progress);
sendBroadcast(intent);
//判断是否是暂停状态
if(isPause){
Log.e(TAG, "down: 当前暂停了" );
downLoadDao.update(info.getUrl(),now);
return;
}
}
Log.e(TAG, "down: 下载结束" );
downLoadDao.delete(info.getUrl());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();
try {
randomFile.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上面是单线程的下载,多线程的下载也是一样的,指定好每个线程要下载的字节部分,即可