android之網絡資源多線程下載


package com.example.threaddown;

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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

EditText url;
EditText target;
Button downBn;
ProgressBar bar;
DownUtil downUtil;
private int mDownStatus;

@Override
protected 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.downBn);
bar = (ProgressBar) findViewById(R.id.br);
// 創建一個Handler對象
final Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
bar.setProgress(mDownStatus);
}
}

};
downBn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// 初始化DownUtil對象
downUtil = new DownUtil(url.getText().toString(), target
.getText().toString(), 6);
new Thread() {

@Override
public void run() {
try {
// 開始下載
downUtil.download();

} catch (Exception e) {
e.printStackTrace();
}
// 定義每秒調度獲取一次系統的完成進度
final Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
// 獲取下載任務的完成比例
double completeRate = downUtil
.getCompleteRate();
mDownStatus = (int) (completeRate * 1000);
// 發送消息通知屆滿更新的進度條
handler.sendEmptyMessage(0x123);
// 下載完成之后取消任務進度
if (mDownStatus >= 100) {
timer.cancel();
}
}
}, 0, 1000);
}

}.start();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


package com.example.threaddown;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.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.targetFile = targetFile;
this.threadNum = threadNum;
}

public void download() throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
// 得到文件的大小
fileSize = conn.getContentLength();
conn.disconnect();
int currentPartsSize = fileSize / threadNum + 1;
RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
// 設置本地文件的大小
file.setLength(fileSize);
file.close();
for (int i = 0; i < threadNum; i++) {
// 計算每條線程的下載位置
int startPos = i * currentPartsSize;
// 每個線程使用一個RandomAccessFile進行下載
RandomAccessFile current = new RandomAccessFile(targetFile, "rw");
// 定義該線程的下載位置
current.seek(startPos);
// 創建下載線程
threads[i] = new DownThread(startPos, currentPartsSize, current);
// 啟動線程下載
threads[i].start();
}

}

// 獲取下載的完成百分比
public double getCompleteRate() {
// 統計多條線程已經下載的總大小
int sumSize = 0;
for (int i = 0; i < threadNum; i++) {
sumSize += threads[i].length;
}
return sumSize * 1.0 / fileSize;
}

private class DownThread extends Thread {
// 定義當前線程下載的位置
private int startPos;
// 定義當前線程下載文件的大小
private int currentPartsSize;
// 當前線程下載的文件塊
private RandomAccessFile currentPart;
// 定義該線程已下載的字節數
private int length;

public DownThread(int startPos, int currentPartsSize,
RandomAccessFile currentPart) {
this.startPos = startPos;
this.currentPart = currentPart;
this.currentPartsSize = currentPartsSize;

}

@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
InputStream in = conn.getInputStream();
in.skip(startPos);
int hasRead = 0;
byte[] buffer = new byte[1024];
// 讀取網絡數據,並寫入本地文件
while (length < currentPartsSize
&& (hasRead = in.read(buffer)) > 0) {
currentPart.write(buffer, 0, hasRead);
// 累計該線程下載的總大小
length += hasRead;
}
currentPart.close();
in.close();

} catch (Exception e) {
e.printStackTrace();
}

}

}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
android:id="@+id/url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/3.1.0/ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar" />

<EditText
android:id="@+id/target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/mnt/sdcard/"/>

<Button
android:id="@+id/downBn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="down" />

<ProgressBar
android:id="@+id/br"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

    <!-- 在SD卡中創建與刪除文件的權限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 在SD開中寫入數據的權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 訪問網路的權限 -->
<uses-permission android:name="android.permission.INTERNET" />



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com