您的位置:首页 > 移动开发 > Android开发

Android_线程_多线程下载

2015-07-15 21:31 453 查看
多线程下载图示:








代码:

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
* 多线程下载
*
* 1.获取到服务器端资源文件的大小,并在客户端创建与服务器端资源文件大小相等的文件
*
* 2.根据开启的线程数量把服务器端的资源平均分成若干份
*
* 3.根据http协议去下载
*
* @author 两点水
*
*/
public class MutileThreadDownload {

// 线程的数量
private static int threadCount = 3;
// 每个下载区块的大小
private static long blocksize;
// 正在运行的线程的数量
private static int runningThreadCount;

public static void main(String[] args) throws Exception {

// 服务器文件的路径
String path = "http://localhost:8080/Day4/putty_V0.63.0.0.43510830.exe";
URL url = new URL(path);
// 连接服务器
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) { // 连接服务器成功
// 得到服务器返回的文件的大小
long length = conn.getContentLength();
System.out.println("服务器端资源文件的大小:" + length);

// 在本地创建一个和服务器端资源文件大小相等的空白文件
File file = new File("putty.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw"); // 对文件设置读写的权限
raf.setLength(length); // 设置文件的大小

// 开启若干个线程进行下载(需要常量:开启线程的的数量,每一个线程下载的大小,每一个线程下载的区域)
blocksize = length / threadCount;
runningThreadCount = threadCount;
for (int i = 1; i <= threadCount; i++) {
// 循环开启线程,确定每个线程下载的区域,开始的位置和结束的为位置,文件的大小读写都是从0开始的
long startIndex = (i - 1) * blocksize; // 开始的位置
long endIndex = i * blocksize - 1; // 结束的位置
// 最后一个线程结束的位置比较特殊,所以要处理一下
if (i == threadCount) {
// 最后一个线程,结束的位置是服务器资源文件的大小
endIndex = length - 1;
}
System.out.println("开启线程:" + i + "下载的位置:" + startIndex + "~"
+ endIndex);
new DownloadThread(i, startIndex, endIndex, path).start();
}

}
conn.disconnect();

}

/**
* 下载的线程内部类
*
* @author 两点水
*
*/
private static class DownloadThread extends Thread {

private int threadId; // 线程的ID
private long startIndex; // 线程下载块开始的位置
private long endIndex; // 线程下载块结束的位置
private String path; // 文件的路径

public DownloadThread(int threadId, long startIndex, long endIndex,
String path) {
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.path = path;
}

@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
System.out.println("code=" + code);
InputStream is = conn.getInputStream();
File file = new File("putty.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 指定的位置开始读写
raf.seek(startIndex);
System.out.println("第" + threadId + "个线程:写文件开始的位置:"
+ String.valueOf(startIndex));
int len = 0;
byte[] buffer = new byte[2014];
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
}
is.close();
raf.close();
System.out.println("线程" + threadId + "下载完毕");
} catch (Exception e) {
e.printStackTrace();
}
}

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