一 前言
在上篇文章中《多线程下载》已经介绍过了多线程下载的原理,简单来说就是把一个大文件分为N等份,然后把每一份交给一个线程去下载,但是在下载的过程可能异常关机等情况,开机后,你再次打开应用去下载这个文件时,发现会重新下载这个文件,而不是接着上次结束的断点下载,这样会造成流量的浪费。为了解决这个问题,于是就出现了断点续传,断点续传是以多线程下载为基础来承担下载任务的,但除此之外每个线程还会随时把自己下载的进度保存到本地,当下载任务停止后再重新开始时,每个线程将会从上次的断点继续下载。
嗯,《多线程下载》已经把多线程下载的原理说的很清楚了这里就不详细介绍了,接下来会直接上代码加以说明,最后会提供示例的完整代码。
二 实现
1 创建数据库
public class DBOpenHelper extends SQLiteOpenHelper { private static final String DBNAME = "eric.db"; private static final int VERSION = 1; public DBOpenHelper(Context context) { super(context, DBNAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS filedownlog (id integer primary key autoincrement, downpath varchar(100), threadid INTEGER, downlength INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS filedownlog"); onCreate(db); } }说明,想要把每个线程下载的进度保存到本地,无疑数据库是最好的方式,数据库的关键字段有:资源文件的路径(downpath)、线程ID(threadid)、每个线程下载进度(downlength)。
2 自定义工具类添加数据库增删改查方法
public class FileDBService { private DBOpenHelper openHelper; public FileDBService(Context context) { openHelper = new DBOpenHelper(context); } /** * 获取每条线程已经下载的文件长度 * * @param path * @return */ public Map<Integer, Integer> getData(String path) { SQLiteDatabase db = openHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select threadid, downlength from filedownlog where downpath=?", new String[] { path }); Map<Integer, Integer> data = new HashMap<Integer, Integer>(); while (cursor.moveToNext()) { data.put(cursor.getInt(0), cursor.getInt(1)); } cursor.close(); db.close(); return data; } /** * 保存每条线程已经下载的文件长度 * @param path * @param map */ public void save(String path, Map<Integer, Integer> map) {// int threadid, SQLiteDatabase db = openHelper.getWritableDatabase(); db.beginTransaction(); try { for (Map.Entry<Integer, Integer> entry : map.entrySet()) { db.execSQL("insert into filedownlog(downpath, threadid, downlength) values(?,?,?)", new Object[] { path, entry.getKey(), entry.getValue() }); } db.setTransactionSuccessful(); } finally { db.endTransaction(); } db.close(); } /** * 实时更新每条线程已经下载的文件长度 * @param path */ public void update(String path, int threadId, int pos) { SQLiteDatabase db = openHelper.getWritableDatabase(); db.execSQL( "update filedownlog set downlength=? where downpath=? and threadid=?", new Object[] { pos, path, threadId }); db.close(); } /** * 当文件下载完成后,删除对应的下载记录 * @param path */ public void delete(String path) { SQLiteDatabase db = openHelper.getWritableDatabase(); db.execSQL("delete from filedownlog where downpath=?", new Object[] { path }); db.close(); } }说明,这里主要是用数据库的SQL语句实现增删改查。
3 获取资源文件的大小并计算每个线程需要承担的下载任务
public FileDownloader(Context context, String downloadUrl, File fileSaveDir, int threadNum) { try { this.context = context; this.downloadUrl = downloadUrl; fileService = new FileDBService(this.context); URL url = new URL(this.downloadUrl); if (!fileSaveDir.exists()) fileSaveDir.mkdirs(); this.threads = new DownloadThread[threadNum]; HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("GET"); conn.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, */*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Referer", downloadUrl); conn.setRequestProperty("Charset", "UTF-8"); conn.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)"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); printResponseHeader(conn); if (conn.getResponseCode() == 200) {//连接成功 this.fileSize = conn.getContentLength();// 获取资源文件的大小 if (this.fileSize <= 0){ throw new RuntimeException("Unkown file size "); } String filename = getFileName(conn);//获取文件名 this.saveFile = new File(fileSaveDir, filename);//构建保存文件 Map<Integer, Integer> logdata = fileService.getData(downloadUrl);//获取下载记录 if (logdata.size() > 0) {//如果存在下载记录 for (Map.Entry<Integer, Integer> entry : logdata.entrySet()){ data.put(entry.getKey(), entry.getValue()); //把各条线程已经下载的数据长度放入data中 } } if (this.data.size() == this.threads.length) {//下面计算所有线程已经下载的数据长度 for (int i = 0; i < this.threads.length; i++) { this.downloadSize += this.data.get(i + 1); } print("已经下载的长度" + this.downloadSize); } //计算每条线程下载的数据长度 this.blockLength = (this.fileSize % this.threads.length) == 0 ? this.fileSize / this.threads.length : this.fileSize / this.threads.length + 1; } else { throw new RuntimeException("server no response "); } } catch (Exception e) { print(e.toString()); throw new RuntimeException("don't connection this url"); } }说明,上述代码主要有两个作用:(1)获取资源文件总的大小;(2)计算每个线程需要承担的下载任务;。在这里计算每个线程要承担的下载任务与前面提到的多线程下载一样。
4 自定义子线程
public class DownloadThread extends Thread { private static final String TAG = "DownloadThread"; /**保存文件的位置*/ private File saveFile; /**文件资源路径*/ private URL downUrl; /**当前线程需要下载的长度*/ private int blockLength; /** 当前线程的id */ private int threadId = -1; /**当前线程已经下载的文件大小*/ private int downLength; /**true 下载完成 否则没有下载完成*/ private boolean finish = false; private FileDownloader downloader; /** * 构造函数 * @param downloader FileDownloader * @param downUrl 资源文件的URL * @param saveFile 构造的本地文件 * @param block 当前线程需要下载的长度 * @param downLength 当前线程已经下载的文件大小 * @param threadId 当前线程ID */ public DownloadThread(FileDownloader downloader, URL downUrl, File saveFile, int block, int downLength, int threadId) { this.downUrl = downUrl; this.saveFile = saveFile; this.blockLength = block; this.downloader = downloader; this.threadId = threadId; this.downLength = downLength; } @Override public void run() { if (downLength < blockLength) { try { HttpURLConnection http = (HttpURLConnection) downUrl.openConnection(); http.setConnectTimeout(5 * 1000); http.setRequestMethod("GET"); http.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, */*"); http.setRequestProperty("Accept-Language", "zh-CN"); http.setRequestProperty("Referer", downUrl.toString());// 先前网页的地址,当前请求网页紧随其后,即来路 http.setRequestProperty("Charset", "UTF-8"); int startPos = blockLength * (threadId - 1) + downLength; int endPos = blockLength * threadId - 1;// 结束位置 http.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);//设置获取实体数据的范围 http.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)"); http.setRequestProperty("Connection", "Keep-Alive"); //需要持久连接 InputStream inStream = http.getInputStream(); byte[] buffer = new byte[1024]; int offset = 0; print("Thread " + this.threadId + " start download from position " + startPos); RandomAccessFile threadfile = new RandomAccessFile(this.saveFile, "rwd"); // 定位下载位置 threadfile.seek(startPos); while (!downloader.getExit() && (offset = inStream.read(buffer, 0, 1024)) != -1) { threadfile.write(buffer, 0, offset); downLength += offset; downloader.update(this.threadId, downLength); downloader.append(offset); } threadfile.close(); inStream.close(); print("Thread " + this.threadId + " download finish"); this.finish = true; } catch (Exception e) { this.downLength = -1; print("Thread " + this.threadId + ":" + e); } } } private static void print(String msg) { Log.i(TAG, msg); } /** * 判断是否已经下载完 * * @return */ public boolean isFinish() { return finish; } /** * 已经下载的文件大小 */ public long getDownLength() { return downLength; } }说明,和多线程下载一样,不再累述,唯一不同的是线程开始下载的位置不再是:
int startPos = blockLength * (threadId - 1)
而是
int startPos = blockLength * (threadId - 1) + downLength;其中 blockLength为每个线程应该承担的下载任务,threadId为线程ID,downLength为该线程已经下载的大小(这个数据是数据库取出的)。
5 开启线程组开始下载
public int download(DownloadProgressListener listener) throws Exception { try { RandomAccessFile randOut = new RandomAccessFile(this.saveFile, "rw"); if (this.fileSize > 0){ randOut.setLength(this.fileSize); // 设置文件的大小 } randOut.close(); URL url = new URL(this.downloadUrl); if (this.data.size() != this.threads.length) { this.data.clear(); for (int i = 0; i < this.threads.length; i++) { this.data.put(i + 1, 0);// 初始化缓存各线程下载的长度 } this.downloadSize = 0; } for (int i = 0; i < this.threads.length; i++) { int downLength = this.data.get(i + 1); if (downLength < this.blockLength && this.downloadSize < this.fileSize) { this.threads[i] = new DownloadThread(this, url, this.saveFile, this.blockLength, this.data.get(i + 1), i + 1); this.threads[i].setPriority(7); // 设置下载线程的优先级 this.threads[i].start(); } else { this.threads[i] = null; } } fileService.delete(this.downloadUrl);//删除已有的缓存 fileService.save(this.downloadUrl, this.data); boolean notFinish = true;//下载未完成 while (notFinish) {// 循环判断所有线程是否完成下载 Thread.sleep(900); notFinish = false;//假定全部线程下载完成 for (int i = 0; i < this.threads.length; i++) { if (this.threads[i] != null && !this.threads[i].isFinish()) {//如果发现线程未完成下载 notFinish = true;//设置标志为下载没有完成 if (this.threads[i].getDownLength() == -1) {//如果下载失败,再重新下载 this.threads[i] = new DownloadThread(this, url, this.saveFile, this.blockLength, this.data.get(i + 1), i + 1); this.threads[i].setPriority(7); this.threads[i].start(); } } } if (listener != null){ listener.onDownloadSize(this.downloadSize);//通知目前已经下载完成的数据长度 } } if (downloadSize == this.fileSize){ fileService.delete(this.downloadUrl);// 下载完成删除记录 } } catch (Exception e) { print(e.toString()); throw new Exception("file download error"); } return this.downloadSize; }说明,其中DownloadProgressListener是进度监听接口,其定义如下:
public interface DownloadProgressListener { public void onDownloadSize(int size); }
示例
public class MainActivity extends Activity { private static final int PROCESSING = 1; private static final int FAILURE = -1; private EditText pathText; private TextView resultView; private Button downloadButton; private Button stopButton; /**进度条*/ private ProgressBar progressBar; private Handler handler = new UIHandler(); /** * 当Handler被创建会关联到创建它的当前线程的消息队列,该类用于往消息队列发送消息 * 消息队列中的消息由当前线程内部进行处理 */ private final class UIHandler extends Handler { public void handleMessage(Message msg) { switch (msg.what) { case PROCESSING: // progressBar.setProgress(msg.getData().getInt("size")); //已经下载的占总的大小的百分比 float fraction = (float) progressBar.getProgress() / (float) progressBar.getMax(); //当前已经下载的大小 int currentLength = (int) (fraction * 100); resultView.setText(currentLength + "%"); if (progressBar.getProgress() == progressBar.getMax()) { Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); } break; case FAILURE: // 下载失败 Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show(); break; } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pathText = (EditText) findViewById(R.id.path); resultView = (TextView) findViewById(R.id.resultView); downloadButton = (Button) findViewById(R.id.downloadbutton); stopButton = (Button) findViewById(R.id.stopbutton); progressBar = (ProgressBar) findViewById(R.id.progressBar); ButtonClickListener listener = new ButtonClickListener(); downloadButton.setOnClickListener(listener); stopButton.setOnClickListener(listener); pathText.setText("http://img3.iqilu.com/data/attachment/forum/201308/21/192654ai88zf6zaa60zddo.jpg"); } private final class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.downloadbutton: //开始下载 String path = pathText.getText().toString(); String filename = path.substring(path.lastIndexOf('/') + 1); try { filename = URLEncoder.encode(filename, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } path = path.substring(0, path.lastIndexOf("/") + 1) + filename; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File savDir = Environment.getExternalStorageDirectory(); download(path, savDir); } else { Toast.makeText(getApplicationContext(), R.string.sdcarderror, Toast.LENGTH_LONG).show(); } downloadButton.setEnabled(false); stopButton.setEnabled(true); break; case R.id.stopbutton: // 停止下载 exit(); Toast.makeText(getApplicationContext(), "Now thread is Stopping!!", Toast.LENGTH_LONG).show(); downloadButton.setEnabled(true); stopButton.setEnabled(false); break; } } private DownloadTask task; private void exit() { if (task != null) task.exit(); } private void download(String path, File savDir) { task = new DownloadTask(path, savDir); new Thread(task).start(); } private final class DownloadTask implements Runnable { private String path; private File saveDir; private FileDownloader loader; public DownloadTask(String path, File saveDir) { this.path = path; this.saveDir = saveDir; } public void exit() { if (loader != null) loader.exit(); } DownloadProgressListener downloadProgressListener = new DownloadProgressListener() { @Override public void onDownloadSize(int size) { Message msg = new Message(); msg.what = PROCESSING; msg.getData().putInt("size", size); handler.sendMessage(msg); } }; @Override public void run() { try { //构造下载器 loader = new FileDownloader(getApplicationContext(), path, saveDir, 3); progressBar.setMax(loader.getFileSize()); //开始下载 loader.download(downloadProgressListener); } catch (Exception e) { e.printStackTrace(); handler.sendMessage(handler.obtainMessage(FAILURE)); } } } } }效果图:
源码贡献
multiThreadDownload完整源码(点击查看)
断点续传的源码在HttpProtocolApp下的 multiThreadDownload中。
文献参考:
http://blog.csdn.net/lu1024188315/article/details/51803300
http://blog.csdn.net/lu1024188315/article/details/51803471