【移動應(yīng)用開發(fā)技術(shù)】Android 使用HttpURLConnection 實現(xiàn)多線程下載_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android 使用HttpURLConnection 實現(xiàn)多線程下載_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android 使用HttpURLConnection 實現(xiàn)多線程下載_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android 使用HttpURLConnection 實現(xiàn)多線程下載_第4頁
【移動應(yīng)用開發(fā)技術(shù)】Android 使用HttpURLConnection 實現(xiàn)多線程下載_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】Android使用HttpURLConnection實現(xiàn)多線程下載

工具類代碼:

package

com.example.xiaocool.multithreadclient;

import

java.io.InputStream;

import

java.io.RandomAccessFile;

import

.HttpURLConnection;

import

.URL;

public

class

DownUtil

{

//

定義下載資源的路徑

private

String

path;

//

指定所下載的文件的保存位置

private

String

targetFile;

//

定義需要使用多少線程下載資源

private

int

threadNum;

//

定義下載的線程對象

private

DownThread[]

threads;

//

定義下載的文件的總大小

private

int

fileSize;

public

DownUtil(String

path,

String

targetFile,

int

threadNum)

{

this.path

=

path;

this.threadNum

=

threadNum;

//

初始化threads數(shù)組

threads

=

new

DownThread[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",

"p_w_picpath/gif,

p_w_picpath/jpeg,

p_w_picpath/pjpeg,

p_w_picpath/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("Charset",

"UTF-8");

conn.setRequestProperty("Connection",

"Keep-Alive");

//

得到文件大小

fileSize

=

conn.getContentLength();

conn.disconnect();

int

currentPartSize

=

fileSize

/

threadNum

+

1;

RandomAccessFile

file

=

new

RandomAccessFile(targetFile,

"rw");

//

設(shè)置本地文件的大小

file.setLength(fileSize);

file.close();

for

(int

i

=

0;

i

<

threadNum;

i++)

{

//

計算每條線程的下載的開始位置

int

startPos

=

i

*

currentPartSize;

//

每個線程使用一個RandomAccessFile進行下載

RandomAccessFile

currentPart

=

new

RandomAccessFile(targetFile,

"rw");

//

定位該線程的下載位置

currentPart.seek(startPos);

//

創(chuàng)建下載線程

threads[i]

=

new

DownThread(startPos,

currentPartSize,

currentPart);

//

啟動下載線程

threads[i].start();

}

}

//

獲取下載的完成百分比

public

double

getCompleteRate()

{

//

統(tǒng)計多條線程已經(jīng)下載的總大小

int

sumSize

=

0;

for

(int

i

=

0;

i

<

threadNum;

i++)

{

sumSize

+=

threads[i].length;

}

//

返回已經(jīng)完成的百分比

return

sumSize

*

1.0

/

fileSize;

}

private

class

DownThread

extends

Thread

{

//

當前線程的下載位置

private

int

startPos;

//

定義當前線程負責下載的文件大小

private

int

currentPartSize;

//

當前線程需要下載的文件塊

private

RandomAccessFile

currentPart;

//

定義已經(jīng)該線程已下載的字節(jié)數(shù)

public

int

length;

public

DownThread(int

startPos,

int

currentPartSize,

RandomAccessFile

currentPart)

{

this.startPos

=

startPos;

this.currentPartSize

=

currentPartSize;

this.currentPart

=

currentPart;

}

@Override

public

void

run()

{

try

{

URL

url

=

new

URL(path);

HttpURLConnection

conn

=

(HttpURLConnection)url

.openConnection();

conn.setConnectTimeout(5

*

1000);

conn.setRequestMethod("GET");

conn.setRequestProperty(

"Accept",

"p_w_picpath/gif,

p_w_picpath/jpeg,

p_w_picpath/pjpeg,

p_w_picpath/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("Charset",

"UTF-8");

InputStream

inStream

=

conn.getInputStream();

//

跳過startPos個字節(jié),表明該線程只下載自己負責哪部分文件。

inStream.skip(this.startPos);

byte[]

buffer

=

new

byte[1024];

int

hasRead

=

0;

//

讀取網(wǎng)絡(luò)數(shù)據(jù),并寫入本地文件

while

(length

<

currentPartSize

&&

(hasRead

=

inStream.read(buffer))

>

0)

{

currentPart.write(buffer,

0,

hasRead);

//

累計該線程下載的總大小

length

+=

hasRead;

}

currentPart.close();

inStream.close();

}

catch

(Exception

e)

{

e.printStackTrace();

}

}

}

}MainActivity:package

com.example.xiaocool.multithreadclient;

import

android.os.Handler;

import

android.os.Message;

import

android.support.v7.app.ActionBarActivity;

import

android.os.Bundle;

import

android.view.Menu;

import

android.view.MenuItem;

import

android.view.View;

import

android.widget.Button;

import

android.widget.EditText;

import

android.widget.ProgressBar;

import

java.util.Timer;

import

java.util.TimerTask;

public

class

MainActivity

extends

ActionBarActivity

{

EditText

url;

EditText

target;

Button

downBn;

ProgressBar

bar;

DownUtil

downUtil;

private

int

mDownStatus;

@Override

public

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

url

=

(EditText)

findViewById(R.id.url);

target

=

(EditText)

findViewById(R.id.target);

downBn

=

(Button)

findViewById(R.id.down);

bar

=

(ProgressBar)

findViewById(R.id.bar);

//

創(chuàng)建一個Handler對象

final

Handler

handler

=

new

Handler()

{

@Override

public

void

handleMessage(Message

msg)

{

if

(msg.what

==

0x123)

{

bar.setProgress(mDownStatus);

}

}

};

downBn.setOnClickListener(new

View.OnClickListener()

{

@Override

public

void

onClick(View

v)

{

//

初始化DownUtil對象

最后一個參數(shù)指定線程數(shù)

downUtil

=

new

DownUtil(url.getText().toString(),

target.getText().toString(),

6);

new

Thread()

{

@Override

public

void

run()

{

try

{

//

開始下載

downUtil.download();

}

catch

(Exception

e)

{

e.printStackTrace();

}

//

定義美妙調(diào)度獲取一次系統(tǒng)的完成進度

final

Timer

timer

=

new

Timer();

timer

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論