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
![](http://static.javashuo.com/static/loading.gif)
注意:因爲Android移動設備和PC機的處理器仍是不能相比,因此開闢的子線程建議不要多於5條。固然如今某些高端機子的處理器能力比較強了,就能夠多開闢幾條子線程。android
二、爲了實現斷點下載,採用數據庫方式記錄下載的進度,這樣當你將該應用退出後,下次點擊下載的時候,程序會去查看該下載連接是否存在下載記錄,若是存在下載記錄就會判斷下載的進度,如何從上次下載的進度繼續開始下載。正則表達式
三、特別注意在主線程裏不能執行一件比較耗時的工做,不然會因主線程阻塞而沒法處理用戶的輸入事件,致使「應用無響應」錯誤的出現。耗時的工做應該在子線程裏執行。sql
四、UI控件畫面的重繪(更新)是由主線程負責處理的,不能在子線程中更新UI控件的值。能夠採用Handler機制,在主線程建立Handler對象,在子線程發送消息給主線程所綁定的消息隊列,從消息中獲取UI控件的值,而後在主線程中進行UI控件的重繪(更新)工做。
五、瞭解HTTP協議各個頭字段的含義數據庫
2、將該下載器的具體實現代碼展示出來數組
step一、首先查看整個Android項目的結構圖瀏覽器
![](http://static.javashuo.com/static/loading.gif)
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服務器上,以供下載。
![](http://static.javashuo.com/static/loading.gif)
step6:部署應用,觀看運行效果
一、打開應用
![](http://static.javashuo.com/static/loading.gif)
二、點擊「開始下載」
![](http://static.javashuo.com/static/loading.gif)
3.點擊「中止下載」
![](http://static.javashuo.com/static/loading.gif)
4.點擊「開始下載」 會繼續上一次的下載進度繼續下載
![](http://static.javashuo.com/static/loading.gif)
5.退出應用,再進應用
![](http://static.javashuo.com/static/loading.gif)
六、點擊「開始下載」,會繼續上一次退出應用的時候的下載進度繼續下載,完成斷點下載
![](http://static.javashuo.com/static/loading.gif)
7.當下載完成的時候
![](http://static.javashuo.com/static/loading.gif)
==================================================================================================
做者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址: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://static.javashuo.com/static/loading.gif)
==================================================================================================
做者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
博客寫完後,又有讀者提出了修改意見,在這兒特別感謝熱心的讀者給的建議,下面是讀者JavaLover00000 給的建議:
![](http://static.javashuo.com/static/loading.gif)
我修改了部分代碼後,具體的優化效果以下所示,修改後下載速度確實變快了不少。
修改前的效果:
![](http://static.javashuo.com/static/loading.gif)
修改後的效果:
![](http://static.javashuo.com/static/loading.gif)
具體修改的代碼能夠到如下地址進行下載:Android基於HTTP協議的多線程斷點下載器的實現源碼_第二次優化以後
主要是將一、buffer改成8k
二、由於發現花費在更新數據庫的時間比 read和write加起來的時間都要多一點,因此將更新數據庫進度改成下載線程出現異常的時候更新單個線程進度和FileDownloader中的exit()中更新全部線程進度
代碼修改的地方具體能夠查看源代碼中的FileDownloader.java和DownloadThread.java
3.HttpURLConnection實現斷點續傳
HttpURLConnection繼承了URLConnection,所以也可用於向指定網站發送GET請求、POST請求,並且它在URLConnection基礎上提供了以下便捷方法:
![](http://static.javashuo.com/static/loading.gif)
實現多線程下載的步驟:
![](http://static.javashuo.com/static/loading.gif)
下邊的總結對我幫助蠻大的~不只用法瞭解,整個鏈接流程也要明白!
原文連接地址:
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 =