您的位置:首页 > 理论基础 > 计算机网络

Android HttpURLConnection 多线程下载

2017-08-22 15:19 239 查看


DownUtil.java

package shortcut.song.com.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Created by Administrator on 2017/8/22 0022.
*/

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;
threads = new DownThread[threadNum];
}

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, 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("Charset", "UTF-8");
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进行下载
RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
// 定位线程的下载位置
currentPart.seek(startPos);
// 创建下载线程
threads[i] = new DownThread(startPos, currentPartSize, currentPart);
// 启动下载线程
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 currentPartSize;
// 当前线程需要下载的文件块
private RandomAccessFile currentPart;
// 定义该线程已下载的字节数
public int length;

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

@Override
public void run() {
super.run();
try {
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, 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("Charset", "UTF-8");
InputStream inStream = conn.getInputStream();
// 跳过startPos 个字节,表明该线程只下载自已负责的那部分文件
skipFully(inStream, this.startPos);
//inStream.skip(this.startPos);
byte[] buffer = new byte[1024];
int hasRead = 0;
while (length < currentPartSize && (hasRead = inStream.read(buffer)) > 0) {
currentPart.write(buffer, 0, hasRead);
// 累计该线程下载的总大小
length += hasRead;
}
currentPart.close();
inStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}

// 定义一个为InputStream跳过bytes字节的方法
public static void skipFully(InputStream in , long bytes) throws IOException {
long remainning =  bytes;
long len = 0;
while (remainning > 0) {
len = in.skip(remainning);
remainning -= len;
}
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="shortcut.song.com.myapplication.HttpURLConnectionTestActivity">

<EditText
android:id="@+id/edit_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Download URL"/>
<EditText
android:id="@+id/edit_target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="file"/>
<ProgressBar
android:id="@+id/progbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:progress="0"/>
<NumberPicker
android:id="@+id/number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"/>
</LinearLayout>


package shortcut.song.com.myapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.ProgressBar;

import java.util.Timer;
import java.util.TimerTask;

import shortcut.song.com.utils.DownUtil;

public class HttpURLConnectionTestActivity extends AppCompatActivity {
EditText url;
EditText target;
Button downbtn;
ProgressBar bar;
NumberPicker numberPicker;
int threadNum = 1;
DownUtil downUtil;
private int mDownStatus;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_urlconnection_test);

url = (EditText)findViewById(R.id.edit_url);
target = (EditText)findViewById(R.id.edit_target);
bar = (ProgressBar)findViewById(R.id.progbar);
downbtn = (Button)findViewById(R.id.btn_download);
numberPicker = (NumberPicker)findViewById(R.id.number_picker);
numberPicker.setMinValue(1);
numberPicker.setMaxValue(20);
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
threadNum = newVal;
}
});

// 创建一个Handler对象
final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
bar.setProgress(mDownStatus);
}
}
};

downbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 初始化DownUtil对象(最后一个参数指定线程数)
downUtil = new DownUtil(url.getText().toString(), target.getText().toString(), threadNum);
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 * 100);
// 发送消息通知界面更新进度
mHandler.sendEmptyMessage(0x123);
// 下载完全后取消任务调度
if (mDownStatus >= 100) {
timer.cancel();
}
}
},0, 100);
}
}.start();
}
});

}
}


运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: