1.简单一例子-Android经过HTTP协议实现多线程下载
- import java.io.File;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- public class MulThreadDownload {
-
-
-
-
- public static void main(String[] args) {
- String path = "http://net.itcast.cn/QQWubiSetup.exe";
- try {
- new MulThreadDownload().download(path, 3);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
- public static String getFilename(String path){
- return path.substring(path.lastIndexOf('/')+1);
- }
-
-
-
-
-
- public void download(String path, int threadsize) throws Exception{
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5 * 1000);
- int filelength = conn.getContentLength();
- String filename = getFilename(path);
- File saveFile = new File(filename);
- RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
- accessFile.setLength(filelength);
- accessFile.close();
-
- int block = filelength%threadsize==0? filelength/threadsize : filelength/threadsize+1;
- for(int threadid=0 ; threadid < threadsize ; threadid++){
- new DownloadThread(url, saveFile, block, threadid).start();
- }
- }
-
- private final class DownloadThread extends Thread{
- private URL url;
- private File saveFile;
- private int block;
- private int threadid;
-
- public DownloadThread(URL url, File saveFile, int block, int threadid) {
- this.url = url;
- this.saveFile = saveFile;
- this.block = block;
- this.threadid = threadid;
- }
-
- @Override
- public void run() {
-
-
- int startposition = threadid * block;
- int endposition = (threadid + 1 ) * block - 1;
- try {
- RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
- accessFile.seek(startposition);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5 * 1000);
- conn.setRequestProperty("Range", "bytes="+ startposition+ "-"+ endposition);
- InputStream inStream = conn.getInputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while( (len=inStream.read(buffer)) != -1 ){
- accessFile.write(buffer, 0, len);
- }
- inStream.close();
- accessFile.close();
- System.out.println("线程id:"+ threadid+ "下载完成");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- }
2.Android基于HTTP协议的多线程断点下载器的实现
1、首先写这篇文章以前,要了解实现该Android多线程断点下载器的几个知识点html
1.多线程下载的原理,以下图所示java

注意:因为Android移动设备和PC机的处理器仍是不能相比,因此开辟的子线程建议不要多于5条。固然如今某些高端机子的处理器能力比较强了,就能够多开辟几条子线程。android
二、为了实现断点下载,采用数据库方式记录下载的进度,这样当你将该应用退出后,下次点击下载的时候,程序会去查看该下载连接是否存在下载记录,若是存在下载记录就会判断下载的进度,如何从上次下载的进度继续开始下载。正则表达式
三、特别注意在主线程里不能执行一件比较耗时的工做,不然会因主线程阻塞而没法处理用户的输入事件,致使“应用无响应”错误的出现。耗时的工做应该在子线程里执行。sql
四、UI控件画面的重绘(更新)是由主线程负责处理的,不能在子线程中更新UI控件的值。能够采用Handler机制,在主线程建立Handler对象,在子线程发送消息给主线程所绑定的消息队列,从消息中获取UI控件的值,而后在主线程中进行UI控件的重绘(更新)工做。
五、了解HTTP协议各个头字段的含义数据库
2、将该下载器的具体实现代码展示出来数组
step一、首先查看整个Android项目的结构图浏览器

step2:设计应用的UI界面 /layout/activity_main.xml缓存
- <span style="font-size:18px"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/path" />
-
- <EditText
- android:id="@+id/path"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="http://192.168.1.100:8080/Hello/a.mp4" />
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
-
- <Button
- android:id="@+id/downloadbutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/startbutton" />
-
- <Button
- android:id="@+id/stopbutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:enabled="false"
- android:text="@string/stopbutton" />
- </LinearLayout>
-
- <ProgressBar
- android:id="@+id/progressBar"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="fill_parent"
- android:layout_height="18dp" />
-
- <TextView
- android:id="@+id/resultView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center" />
- </LinearLayout></span>
/values/string.xml服务器
- <span style="font-size:18px"><?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="action_settings">Settings</string>
- <string name="hello_world">Hello world!</string>
- <string name="app_name">多线程断点下载器_欧阳鹏编写</string>
- <string name="path">下载路径</string>
- <string name="startbutton">开始下载</string>
- <string name="success">下载完成</string>
- <string name="error">下载失败</string>
- <string name="stopbutton">中止下载</string>
- <string name="sdcarderror">SDCard不存在或者写保护</string>
- </resources></span>
step三、程序主应用 cn.oyp.download.MainActivity.java文件
- <span style="font-size:18px">package cn.oyp.download;
-
- import java.io.File;
-
- 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;
- import cn.oyp.download.downloader.DownloadProgressListener;
- import cn.oyp.download.downloader.FileDownloader;
-
- public class MainActivity extends Activity {
-
-
- private EditText pathText;
-
- private Button downloadButton;
-
- private Button stopbutton;
-
- private ProgressBar progressBar;
-
- private TextView resultView;
-
-
- private Handler handler = new UIHander();
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- pathText = (EditText) this.findViewById(R.id.path);
- downloadButton = (Button) this.findViewById(R.id.downloadbutton);
- stopbutton = (Button) this.findViewById(R.id.stopbutton);
- progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
- resultView = (TextView) this.findViewById(R.id.resultView);
-
- ButtonClickListener listener = new ButtonClickListener();
- downloadButton.setOnClickListener(listener);
- stopbutton.setOnClickListener(listener);
- }
-
-
-
-
- private final class UIHander extends Handler {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- int size = msg.getData().getInt("size");
- progressBar.setProgress(size);
-
- float num = (float) progressBar.getProgress()
- / (float) progressBar.getMax();
- int result = (int) (num * 100);
- resultView.setText(result + "%");
-
- if (progressBar.getProgress() == progressBar.getMax()) {
- Toast.makeText(getApplicationContext(), R.string.success, 1)
- .show();
- }
- break;
- case -1:
- Toast.makeText(getApplicationContext(), R.string.error, 1)
- .show();
- break;
- }
- }
- }
-
-
-
-
- private final class ButtonClickListener implements View.OnClickListener {
- public void onClick(View v) {
- switch (v.getId()) {
-
- case R.id.downloadbutton:
- String path = pathText.getText().toString();
-
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) {
-
- File saveDir = Environment.getExternalStorageDirectory();
-
- download(path, saveDir);
- } else {
- Toast.makeText(getApplicationContext(),
- R.string.sdcarderror, 1).show();
- }
- downloadButton.setEnabled(false);
- stopbutton.setEnabled(true);
- break;
-
- case R.id.stopbutton:
- exit();
- downloadButton.setEnabled(true);
- stopbutton.setEnabled(false);
- break;
- }
- }
-
-
-
-
-
-
- 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 run() {
- try {
-
-
-
- loader = new FileDownloader(getApplicationContext(), path,
- saveDir, 5);
- progressBar.setMax(loader.getFileSize());
-
-
-
-
-
- loader.download(new DownloadProgressListener() {
- public void onDownloadSize(int size) {
-
- Message msg = new Message();
- msg.what = 1;
- msg.getData().putInt("size", size);
- handler.sendMessage(msg);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
-
- handler.sendMessage(handler.obtainMessage(-1));
- }
- }
-
-
-
-
- public void exit() {
- if (loader != null)
- loader.exit();
- }
- }
-
-
-
-
-
-
-
-
-
- private DownloadTask task;
-
-
-
-
- public void exit() {
- if (task != null)
- task.exit();
- }
-
-
-
-
-
-
-
-
-
- private void download(String path, File saveDir) {
- task = new DownloadTask(path, saveDir);
- new Thread(task).start();
- }
- }
-
- }
- </span>
文件下载器cn.oyp.download.downloader.FileDownloader.java文件
- <span style="font-size:18px">package cn.oyp.download.downloader;
-
- 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 cn.oyp.download.service.FileService;
-
- import android.content.Context;
- import android.util.Log;
-
-
-
-
- public class FileDownloader {
- private static final String TAG = "FileDownloader";
-
- private Context context;
-
- private FileService fileService;
-
- private boolean exit;
-
- private int downloadSize = 0;
-
- private int fileSize = 0;
-
- private DownloadThread[] threads;
-
- private File saveFile;
-
- private Map<Integer, Integer> data = new ConcurrentHashMap<Integer, Integer>();
-
- private int block;
-
- private String downloadUrl;
-
-
-
-
- public int getThreadSize() {
- return threads.length;
- }
-
-
-
-
- public void exit() {
- this.exit = true;
- }
-
-
-
-
- public boolean getExit() {
- return this.exit;
- }
-
-
-
-
- public int getFileSize() {
- return fileSize;
- }
-
-
-
-
-
- protected synchronized void append(int size) {
- downloadSize += size;
- }
-
-
-
-
-
-
-
-
-
- protected synchronized void update(int threadId, int pos) {
-
- this.data.put(threadId, pos);
-
- this.fileService.update(this.downloadUrl, threadId, pos);
- }
-
-
-
-
-
-
-
-
-
-
-
- public FileDownloader(Context context, String downloadUrl,
- File fileSaveDir, int threadNum) {
- try {
- this.context = context;
- this.downloadUrl = downloadUrl;
- fileService = new FileService(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());
- }
- 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.block = (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");
- }
- }
-
- /**
- * 获取文件名
- *
- * @param conn
- * Http链接
- */
- private String getFileName(HttpURLConnection conn) {
- String filename = this.downloadUrl.substring(this.downloadUrl
- .lastIndexOf('/') + 1);
-
- if (filename == null || "".equals(filename.trim())) {
-
- for (int i = 0;; i++) {
- String mine = conn.getHeaderField(i);
- if (mine == null)
- break;
-
-
-
-
-
-
-
- if ("content-disposition".equals(conn.getHeaderFieldKey(i)
- .toLowerCase())) {
-
- Matcher m = Pattern.compile(".*filename=(.*)").matcher(
- mine.toLowerCase());
-
- if (m.find())
- return m.group(1);
- }
- }
-
- filename = UUID.randomUUID() + ".tmp";
- }
- return filename;
- }
-
-
-
-
-
-
-
-
-
- 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.block
- && this.downloadSize < this.fileSize) {
-
- this.threads[i] = new DownloadThread(this, url,
- this.saveFile, this.block, 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.block,
- 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;
- }
-
-
-
-
-
-
- public static Map<String, String> getHttpResponseHeader(
- HttpURLConnection http) {
- Map<String, String> header = new LinkedHashMap<String, String>();
- for (int i = 0;; i++) {
- String mine = http.getHeaderField(i);
- if (mine == null)
- break;
- header.put(http.getHeaderFieldKey(i), mine);
- }
- return header;
- }
-
-
-
-
-
-
- public static void printResponseHeader(HttpURLConnection http) {
- Map<String, String> header = getHttpResponseHeader(http);
- for (Map.Entry<String, String> entry : header.entrySet()) {
- String key = entry.getKey() != null ? entry.getKey() + ":" : "";
- print(key + entry.getValue());
- }
- }
-
-
-
-
- private static void print(String msg) {
- Log.i(TAG, msg);
- }
- }
- </span>
文件下载线程 cn.oyp.download.downloader.DownloadThread.java文件
- <span style="font-size:18px">package cn.oyp.download.downloader;
-
-
- import java.io.File;
- 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 File saveFile;
-
- private URL downUrl;
-
- private int block;
-
- private int threadId = -1;
-
- private int downLength;
-
- private boolean finish = false;
-
- private FileDownloader downloader;
-
-
-
- public DownloadThread(FileDownloader downloader, URL downUrl,
- File saveFile, int block, int downLength, int threadId) {
- this.downUrl = downUrl;
- this.saveFile = saveFile;
- this.block = block;
- this.downloader = downloader;
- this.threadId = threadId;
- this.downLength = downLength;
- }
-
-
-
- @Override
- public void run() {
- if (downLength < block) {
- 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 = block * (threadId - 1) + downLength;
-
- int endPos = block * 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);
- /**
- * rwd: 打开以便读取和写入,对于 "rw",还要求对文件内容的每一个更新都同步写入到基础存储设备。
- * 对于Android移动设备必定要注意同步,不然当移动设备断电的话会丢失数据
- */
- 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);
- }
-
-
-
-
-
-
- public boolean isFinish() {
- return finish;
- }
-
-
-
-
-
-
- public long getDownLength() {
- return downLength;
- }
- }
- </span>
下载进度监听接口cn.oyp.download.downloader.DownloadProgressListener.java文件
- <span style="font-size:18px">package cn.oyp.download.downloader;
-
-
-
-
- public interface DownloadProgressListener {
-
-
-
- public void onDownloadSize(int size);
- }
- </span>
数据库操做类 cn.oyp.download.service.DBOpenHelper.java类
- <span style="font-size:18px">package cn.oyp.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 = 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);
- }
- }
- </span>
文件下载服务类cn.oyp.download.service.FileService
- <span style="font-size:18px">package cn.oyp.download.service;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
-
-
-
-
- public class FileService {
- private DBOpenHelper openHelper;
-
- public FileService(Context context) {
- openHelper = new DBOpenHelper(context);
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- public void save(String path, Map<Integer, Integer> map) {
-
- 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();
- }
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
-
- public void delete(String path) {
- SQLiteDatabase db = openHelper.getWritableDatabase();
- db.execSQL("delete from filedownlog where downpath=?",
- new Object[] { path });
- db.close();
- }
- }
- </span>
step4:AndroidManifest.xml
- <span style="font-size:18px"><?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.oyp.download"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
-
- <uses-permission android:name="android.permission.INTERNET" />
-
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
-
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/icon"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="cn.oyp.download.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest></span>
step5:因为便于本项目的展现,因此新建一个JSP项目,部署到Tomcat服务器上,以供下载。

step6:部署应用,观看运行效果
一、打开应用

二、点击“开始下载”

3.点击“中止下载”

4.点击“开始下载” 会继续上一次的下载进度继续下载

5.退出应用,再进应用

六、点击“开始下载”,会继续上一次退出应用的时候的下载进度继续下载,完成断点下载

7.当下载完成的时候

==================================================================================================
做者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
读者下载源码后,会发现下载速度特别慢,有如下两种缘由:
一、因为自己的网络速度的缘由,不会特别快。
二、因为使用RandomAccessFile的缘由,对IO操做太过于频繁。所以,我修改了DownloadThread类,修改代码以下,修改以后对速度有了点提高。在此特别感谢 pobi 读者的意见
- package cn.oyp.download.downloader;
-
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
-
- import android.util.Log;
-
- public class DownloadThread extends Thread {
- private static final String TAG = "DownloadThread";
-
- private File saveFile;
-
- private URL downUrl;
-
- private int block;
-
- private int threadId = -1;
-
- private int downLength;
-
- private boolean finish = false;
-
- private FileDownloader downloader;
-
-
-
-
- public DownloadThread(FileDownloader downloader, URL downUrl,
- File saveFile, int block, int downLength, int threadId) {
- this.downUrl = downUrl;
- this.saveFile = saveFile;
- this.block = block;
- this.downloader = downloader;
- this.threadId = threadId;
- this.downLength = downLength;
- }
-
-
-
-
- @Override
- public void run() {
- if (downLength < block) {
- 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 = block * (threadId - 1) + downLength;
-
- int endPos = block * 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");
- /****/
- System.out.println("DownloadThread http.getResponseCode():"
- + http.getResponseCode());
- if (http.getResponseCode() == 206) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- InputStream inStream = http.getInputStream();
- BufferedInputStream bis = new BufferedInputStream(inStream);
- byte[] buffer = new byte[1024 * 4];
- int offset = 0;
- RandomAccessFile threadfile = new RandomAccessFile(
- this.saveFile, "rwd");
-
- FileChannel outFileChannel = threadfile.getChannel();
-
- outFileChannel.position(startPos);
-
- while (!downloader.getExit()
- && (offset = bis.read(buffer)) != -1) {
- outFileChannel
- .write(ByteBuffer.wrap(buffer, 0, offset));
- downLength += offset;
- downloader.update(this.threadId, downLength);
- downloader.append(offset);
- }
- outFileChannel.close();
- 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);
- }
-
-
-
-
-
-
- public boolean isFinish() {
- return finish;
- }
-
-
-
-
-
-
- public long getDownLength() {
- return downLength;
- }
- }
==================================下面看一个gif动画===========================================
能够查看log日志,查看多线程下载的状况

==================================================================================================
做者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
博客写完后,又有读者提出了修改意见,在这儿特别感谢热心的读者给的建议,下面是读者JavaLover00000 给的建议:

我修改了部分代码后,具体的优化效果以下所示,修改后下载速度确实变快了不少。
修改前的效果:

修改后的效果:

具体修改的代码能够到如下地址进行下载:Android基于HTTP协议的多线程断点下载器的实现源码_第二次优化以后
主要是将一、buffer改成8k
二、由于发现花费在更新数据库的时间比 read和write加起来的时间都要多一点,因此将更新数据库进度改成下载线程出现异常的时候更新单个线程进度和FileDownloader中的exit()中更新全部线程进度
代码修改的地方具体能够查看源代码中的FileDownloader.java和DownloadThread.java
3.HttpURLConnection实现断点续传
HttpURLConnection继承了URLConnection,所以也可用于向指定网站发送GET请求、POST请求,并且它在URLConnection基础上提供了以下便捷方法:

实现多线程下载的步骤:

下边的总结对我帮助蛮大的~不只用法了解,整个链接流程也要明白!
原文连接地址:
http://www.blogjava.net/supercrsky/articles/247449.html
针对JDK中的URLConnection链接Servlet的问题,网上有虽然有所涉及,可是只是说明了某一个或几个问题,是以FAQ的方式来解决的,并且比较零散,如今对这个类的使用就本人在项目中的使用经验作以下总结:
1:> URL请求的类别:
分为二类,GET与POST请求。两者的区别在于:
a:) get请求能够获取静态页面,也能够把参数放在URL字串后面,传递给servlet,
b:) post与get的不一样之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
2:> URLConnection的对象问题:
URLConnection的对象,以下代码示例:
// 下面的index.jsp由<servlet-mapping>映射到
// 一个Servlet(com.quantanetwork.getClientDataServlet)
// 该Servlet的注意点下边会提到
1
URL url
=
new
URL(
"
http://localhost:8080/TestHttpURLConnectionPro/index.jsp
"
);
2
3
URLConnection rulConnection
=
url.openConnection();
4
//
此处的urlConnection对象其实是根据URL的
5
//
请求协议(此处是http)生成的URLConnection类
6
//
的子类HttpURLConnection,故此处最好将其转化
7
//
为HttpURLConnection类型的对象,以便用到
8
//
HttpURLConnection更多的API.以下:
9
10
HttpURLConnection httpUrlConnection
=
(HttpURLConnection) rulConnection;
3:> HttpURLConnection对象参数问题
1
//
设置是否向httpUrlConnection输出,由于这个是post请求,参数要放在
2
//
http正文内,所以须要设为true, 默认状况下是false;
3
httpUrlConnection.setDoOutput(
true
);
4
5
//
设置是否从httpUrlConnection读入,默认状况下是true;
6
httpUrlConnection.setDoInput(
true
);
7
8
//
Post 请求不能使用缓存
9
httpUrlConnection.setUseCaches(
false
);
10
11
//
设定传送的内容类型是可序列化的java对象
12
//
(若是不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
13
httpUrlConnection.setRequestProperty(
"
Content-type
"
,
"
application/x-java-serialized-object
"
);
14
15
//
设定请求的方法为"POST",默认是GET
16
httpUrlConnection.setRequestMethod(
"
POST
"
);
17
18
//
链接,从上述第2条中url.openConnection()至此的配置必需要在connect以前完成,
19
httpUrlConnection.connect();
4:> HttpURLConnection链接问题:
1
//
此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,
2
//
因此在开发中不调用上述的connect()也能够)。
3
OutputStream outStrm
=
httpUrlConnection.getOutputStream();
5:> HttpURLConnection写数据与发送数据问题:
1
//
如今经过输出流对象构建对象输出流对象,以实现输出可序列化的对象。
2
ObjectOutputStream objOutputStrm
=
new
ObjectOutputStream(outStrm);
3
4
//
向对象输出流写出数据,这些数据将存到内存缓冲区中
5
objOutputStrm.writeObject(
new
String(
"
我是测试数据
"
));
6
7
//
刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream)
8
objOutputStm.flush();
9
10
//
关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中,
11
//
在调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器
12
objOutputStm.close();
13
14
//
调用HttpURLConnection链接对象的getInputStream()函数,
15
//
将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
16
InputStream inStrm
=
httpConn.getInputStream();
//
<===注意,实际发送请求的代码段就在这里
17
18
//
上边的httpConn.getInputStream()方法已调用,本次HTTP请求已结束,下边向对象输出流的输出已无心义,
19
//
既使对象输出流没有调用close()方法,下边的操做也不会向对象输出流写入任何数据.
20
//
所以,要从新发送数据时须要从新建立链接、从新设参数、从新建立流对象、从新写数据、
21
//
从新发送数据(至因而否不用从新这些操做须要再研究)
22
objOutputStm.writeObject(
new
String(
""
));
23
httpConn.getInputStream()
总结:a:) HttpURLConnection的connect()函数,实际上只是创建了一个与服务器的tcp链接,并无实际发送http请求。
不管是post仍是get,http请求实际上直到HttpURLConnection的getInputStream()这个函数里面才正式发送出去。
b:) 在用POST方式发送URL请求时,URL请求参数的设定顺序是重中之重,
对connection对象的一切配置(那一堆set函数)
都必需要在connect()函数执行以前完成。而对outputStream的写操做,又必需要在inputStream的读操做以前。
这些顺序其实是由http请求的格式决定的。
若是inputStream读操做在outputStream的写操做以前,会抛出例外:
java.net.ProtocolException: Cannot write output after reading input.......
c:) http请求实际上由两部分组成,
一个是http头,全部关于这次http请求的配置都在http头里面定义,
一个是正文content。
connect()函数会根据HttpURLConnection对象的配置值生成http头部信息,所以在调用connect函数以前,
就必须把全部的配置准备好。
d:) 在http头后面紧跟着的是http请求的正文,正文的内容是经过outputStream流写入的,
实际上outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会当即发送到网络,
而是存在于内存缓冲区中,待outputStream流关闭时,根据输入的内容生成http正文。
至此,http请求的东西已经所有准备就绪。在getInputStream()函数调用的时候,就会把准备好的http请求
正式发送到服务器了,而后返回一个输入流,用于读取服务器对于这次http请求的返回信息。因为http
请求在getInputStream的时候已经发送出去了(包括http头和正文),所以在getInputStream()函数
以后对connection对象进行设置(对http头的信息进行修改)或者写入outputStream(对正文进行修改)
都是没有意义的了,执行这些操做会致使异常的发生。
6:> Servlet端的开发注意点:
a:) 对于客户端发送的POST类型的HTTP请求,Servlet必须实现doPost方法,而不能用doGet方法。
b:) 用HttpServletRequest的getInputStream()方法取得InputStream的对象,好比:
InputStream inStream = httpRequest.getInputStream();
如今调用inStream.available()(该方法用于“返回此输入流下一个方法调用能够不受阻塞地
今后输入流读取(或跳过)的估计字节数”)时,永远都反回0。试图使用此方法的返回值分配缓冲区,
以保存此流全部数据的作法是不正确的。那么,如今的解决办法是
Servlet这一端用以下实现:
InputStream inStream = httpRequest.getInputStream();
ObjectInputStream objInStream = new ObjectInputStream(inStream);
Object obj = objInStream.readObject();
// 作后续的处理
// 。。。。。。
// 。。。 。。。
而客户端,不管是否发送实际数据都要写入一个对象(那怕这个对象不用),如:
ObjectOutputStream objOutputStrm = new ObjectOutputStream(outStrm);
objOutputStrm.writeObject(new String("")); // 这里发送一个空数据
// 甚至能够发一个null对象,服务端取到后再作判断处理。
objOutputStrm.writeObject(null);
objOutputStrm.flush();
objOutputStrm.close();
注意:上述在建立对象输出流ObjectOutputStream时,若是将从HttpServletRequest取得的输入流
(即:new ObjectOutputStream(outStrm)中的outStrm)包装在BufferedOutputStream流里面,
则必须有objOutputStrm.flush();这一句,以便将流信息刷入缓冲输出流.以下:
ObjectOutputStream objOutputStrm = new ObjectOutputStream(new BufferedOutputStream(outStrm));
objOutputStrm.writeObject(null);
objOutputStrm.flush(); // <======此处必需要有.
objOutputStrm.close();
HttpURLConnection是基于HTTP协议的,其底层经过socket通讯实现。若是不设置超时(timeout),在网络异常的状况下,可能会致使程序僵死而不继续往下执行。能够经过如下两个语句来设置相应的超时:
System.setProperty("sun.net.client.defaultConnectTimeout", 超时毫秒数字符串);
System.setProperty("sun.net.client.defaultReadTimeout", 超时毫秒数字符串);
其中: sun.net.client.defaultConnectTimeout:链接主机的超时时间(单位:毫秒)
sun.net.client.defaultReadTimeout:从主机读取数据的超时时间(单位:毫秒)
例如:
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTime
Java中可使用HttpURLConnection来请求WEB资源。
HttpURLConnection对象不能直接构造,须要经过URL.openConnection()来得到HttpURLConnection对象,示例代码以下:
1
String szUrl
=
"
http://www.ee2ee.com/
"
;
2
URL url
=
new
URL(szUrl);
3
HttpURLConnection urlCon
=
(HttpURLConnection)url.openConnection();
HttpURLConnection是基于HTTP协议的,其底层经过socket通讯实现。若是不设置超时(timeout),在网络异常的状况下,可能会致使程序僵死而不继续往下执行。能够经过如下两个语句来设置相应的超时:
System.setProperty("sun.net.client.defaultConnectTimeout", 超时毫秒数字符串);
System.setProperty("sun.net.client.defaultReadTimeout", 超时毫秒数字符串);
其中: sun.net.client.defaultConnectTimeout:链接主机的超时时间(单位:毫秒)
sun.net.client.defaultReadTimeout:从主机读取数据的超时时间(单位:毫秒)
例如:
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
JDK 1.5之前的版本,只能经过设置这两个系统属性来控制网络超时。在1.5中,还可使用HttpURLConnection的父类URLConnection的如下两个方法:
setConnectTimeout:设置链接主机超时(单位:毫秒)
setReadTimeout:设置从主机读取数据超时(单位:毫秒)
例如:
1
HttpURLConnection urlCon
=
(HttpURLConnection)url.openConnection();
2
urlCon.setConnectTimeout(
30000
);
3
urlCon.setReadTimeout(
30000
);
须要注意的是,笔者在JDK1.4.2环境下,发如今设置了defaultReadTimeout的状况下,若是发生网络超时,HttpURLConnection会自动从新提交一次请求,出现一次请求调用,请求服务器两次的问题(Trouble)。我认为这是JDK1.4.2的一个bug。在JDK1.5.0中,此问题已获得解决,不存在自动重发现象。out", "3000
下面用一个示例来示范使用HttpURLConnection实现多线程下载。此代码来源疯狂讲义一书,该代码主要思路:在Activity中点击按钮,调用DownUtil的download()方法,在download()中启动四个线程去下载资源,每一个线程负责下载本身的那部分资源,代码以下:
Activity:
- package com.home.activity;
-
- import java.util.Timer;
- import java.util.TimerTask;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
-
- import com.home.multithreaddown.R;
- import com.home.util.DownUtil;
-
- public class MultiThreadDownActivity extends Activity {
- private EditText urlText;
- private EditText targetText;
- private Button downBtn;
- private ProgressBar bar;
- private DownUtil downUtil;
- private int mDownStatus;
- private Handler handler;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- targetText = (EditText) findViewById(R.id.main_et_name);
- urlText = (EditText) findViewById(R.id.main_et_url);
- downBtn = (Button) findViewById(R.id.main_btn_download);
- bar = (ProgressBar) findViewById(R.id.main_progressBar);
-
- handler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == 0x123) {
- bar.setProgress(mDownStatus);
- }
- }
- };
- downBtn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- downUtil = new DownUtil(urlText.getText().toString(),
- targetText.getText().toString(), 4);
- try {
-
- downUtil.download();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- final Timer timer = new Timer();
- timer.schedule(new TimerTask() {
- public void run() {
-
- double completeRate = downUtil.getCompleteRate();
- mDownStatus = (int) (completeRate * 100);
-
- handler.sendEmptyMessage(0x123);
-
- if (mDownStatus >= 100) {
- timer.cancel();
- }
- }
- }, 0, 100);
-
- }
- });
- }
-
- }
下载的工具类(DownUtil):
- package com.home.util;
-
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- public class DownUtil {
-
- private String path;
-
- private String targetFile;
-
- private int threadNum;
-
- private int fileSize;
-
- private DownloadThread[] threads;
-
- public DownUtil(String path, String targetFile, int threadNum) {
- this.path = path;
- this.threadNum = threadNum;
-
- threads = new DownloadThread[threadNum];
- this.targetFile = targetFile;
- }
-
- public void download() throws Exception {
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5 * 1000);
- conn.setRequestMethod("GET");
- conn.setRequestProperty(
- "Accept",
- "image/gif,image/jpeg,image/pjpeg,application/x-shockwaveflash,application/x-ms-xbap,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*");
- conn.setRequestProperty("Accept-Language", "zh-CN");
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty(
- "User-Agent",
- "Mozilla/4.0(compatible;MSIE7.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");
-
- fileSize = conn.getContentLength();
- conn.disconnect();
- int currentPartSize = fileSize / threadNum + 1;
- RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
-
- file.setLength(fileSize);
- file.close();
- for (int i = 0; i < threadNum; i++) {
-
- int startPos = i * currentPartSize;
-
- RandomAccessFile currentPart = new RandomAccessFile(targetFile,
- "rw");
-
- currentPart.seek(startPos);
-
- threads[i] = new DownloadThread(startPos, currentPartSize,
- currentPart);
-
- threads[i].start();
- }
- }
-
- /**
- * 获取下载完成的百分比
- *
- * @return
- */
- public double getCompleteRate() {
-
- int sumSize = 0;
- for (int i = 0; i < threadNum; i++) {
- sumSize += threads[i].length;
- }
-
- return sumSize * 1.0 / fileSize;
- }
-
- private class DownloadThread extends Thread {
-
- private int startPos;
-
- private int currentPartSize;
-
- private RandomAccessFile currentPart;
-
- private int length = 0;
-
- public DownloadThread(int startPos, int currentPartSize,
- RandomAccessFile currentPart) {
- this.startPos = startPos;
- this.currentPartSize = currentPartSize;
- this.currentPart = currentPart;
- }
-
- public void run() {
- try {
- URL url =