我们编写的是Andorid的HTTP多线程断点下载应用程序。因为之间我们学习的学习积累,直接使用单线程下载HTTP文件对我们来说是一件非常简单的事。那么,多线程断点下载的难点在哪里?1.多线程下载,2.支持断点。
多线程下载:
如何才能从文件的指定位置处开始下载文件?(比如从50MB开始)这一点我们可以通过HTTP请求信息头来设置,还记得HTTP请求信息头的“Range”属性吗?
断点:
首要问题(多线程下载)已经被我们解决了,支持断点下载想必大家也已经想到了。就是将下载的进度保存到文件中,但在Android中却不能这么做。通过老黎的试验,在Android平台中,我们需要向文件中写出下载的文件数据,还需要向另一个文件中写出下载进度,这样会出错。这样会导致有一个文件的内容没有被写出。所以我们就不能以文件的方式来保存下载进度,但可以通过数据库的方式保存下载进度。
这两大问题我们已经有了解决思路,那么就开始动手编写吧!
1.创建Android工程
Project name:MulThreadDownloader
BuildTarget:Android2.1
Application name:多线程断点下载
Package name:com.changcheng.download
Create Activity:MulThreadDownloader
Min SDK Version:7
2.AndroidManifest.xml
3.strings.xml
4.main.xml
5.MulThreadDownloader
package com.changcheng.download; import java.io.File; import com.changcheng.net.download.DownloadProgressListener; import com.changcheng.net.download.FileDownloader; import com.changcheng.download.R; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MulThreadDownloader extends Activity { private EditText pathText; private ProgressBar progressBar; private TextView resultView; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if(!Thread.currentThread().isInterrupted()){ switch (msg.what) { case 1: // 获取当前文件下载的进度 int size = msg.getData().getInt("size"); progressBar.setProgress(size); int result = (int)(((float)size/(float)progressBar.getMax()) * 100); resultView.setText(result+ "%"); if(progressBar.getMax() == size){ Toast.makeText(MulThreadDownloader.this, "文件下载完成", 1).show(); } break; case -1: String error = msg.getData().getString("error"); Toast.makeText(MulThreadDownloader.this, error, 1).show(); break; } } super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pathText = (EditText)this.findViewById(R.id.path); progressBar = (ProgressBar)this.findViewById(R.id.downloadbar); resultView = (TextView)this.findViewById(R.id.resultView); Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String path = pathText.getText().toString(); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //下载文件需要很长的时间,主线程是不能够长时间被阻塞,如果主线程被长时间阻塞, 那么Android被回收应用 download(path, Environment.getExternalStorageDirectory()); }else{ Toast.makeText(MulThreadDownloader.this, R.string.sdcarderror, 1).show(); } } }); } /** * 下载文件 * @param path 下载路径 * @param saveDir 文件保存目录 */ //对于Android的UI控件,只能由主线程负责显示界面的更新,其他线程不能直接更新UI控件的显示 public void download(final String path, final File saveDir){ new Thread(new Runnable() { @Override public void run() { FileDownloader downer = new FileDownloader(MulThreadDownloader.this, path, saveDir, 3); progressBar.setMax(downer.getFileSize());//设置进度条的最大刻度 try { downer.download(new DownloadProgressListener(){ @Override public void onDownloadSize(int size) { Message msg = new Message(); msg.what = 1; msg.getData().putInt("size", size); handler.sendMessage(msg);//发送消息 }}); } catch (Exception e) { Message msg = new Message(); msg.what = -1; msg.getData().putString("error", "下载失败"); handler.sendMessage(msg); } } }).start(); } }
6.FileDownload
package com.changcheng.net.download; import java.io.File; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.util.LinkedHashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.changcheng.download.service.FileService; import android.content.Context; import android.util.Log; /** * 文件下载器 * @author [email protected] * */ public class FileDownloader { private Context context; private FileService fileService; private static final String TAG = "FileDownloader"; /* 已下载文件大小 */ private int downloadSize = 0; /* 原始文件大小 */ private int fileSize = 0; /* 线程数 */ private DownloadThread[] threads; /* 下载路径 */ private URL url; /* 本地保存文件 */ private File saveFile; /* 下载记录文件 */ private File logFile; /* 缓存各线程最后下载的位置*/ private Map
7.DownloadProgressListener
package com.changcheng.net.download; public interface DownloadProgressListener { public void onDownloadSize(int size); }
8.FileService
package com.changcheng.download.service; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * 业务bean * */ public class FileService { private DBOpenHelper openHelper; public FileService(Context context) { openHelper = new DBOpenHelper(context); } /** * 获取线程最后下载位置 * @param path * @return */ public Map
9.DownloadThread
package com.changcheng.net.download; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import android.util.Log; public class DownloadThread extends Thread { private static final String TAG = "DownloadThread"; private RandomAccessFile saveFile; private URL downUrl; private int block; /* 下载开始位置 */ private int threadId = -1; private int startPos; private int downLength; private boolean finish = false; private FileDownloader downloader; public DownloadThread(FileDownloader downloader, URL downUrl, RandomAccessFile saveFile, int block, int startPos, int threadId) { this.downUrl = downUrl; this.saveFile = saveFile; this.block = block; this.startPos = startPos; this.downloader = downloader; this.threadId = threadId; this.downLength = startPos - (block * (threadId - 1)); } @Override public void run() { if(downLength < block){//未下载完成 try { HttpURLConnection http = (HttpURLConnection) downUrl.openConnection(); 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"); http.setRequestProperty("Range", "bytes=" + this.startPos + "-"); 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(); int max = block>1024 ? 1024 : (block>10 ? 10 : 1); byte[] buffer = new byte[max]; int offset = 0; print("线程 " + this.threadId + "从位置"+ this.startPos+ "开始下载 "); while (downLength < block && (offset = inStream.read(buffer, 0, max)) != -1) { saveFile.write(buffer, 0, offset); downLength += offset; downloader.update(this.threadId, block * (threadId - 1) + downLength); downloader.saveLogFile(); downloader.append(offset); int spare = block-downLength;//求剩下的字节数 if(spare < max) max = (int) spare; } saveFile.close(); inStream.close(); print("线程 " + this.threadId + "完成下载 "); this.finish = true; this.interrupt(); } catch (Exception e) { this.downLength = -1; print("线程"+ this.threadId+ ":"+ e); } } } private static void print(String msg){ Log.i(TAG, msg); } /** * 下载是否完成 * @return */ public boolean isFinish() { return finish; } /** * 已经下载的内容大小 * @return 如果返回值为-1,代表下载失败 */ public long getDownLength() { return downLength; } }
11.DBOpenHelper
package com.changcheng.download.service; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { private static final String DBNAME = "download.db"; private static final int VERSION = 2; public DBOpenHelper(Context context) { super(context, DBNAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS filedown (id integer primary key autoincrement, downpath varchar(100), threadid INTEGER, position INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS filedown"); onCreate(db); } }