1. 首先说一下多线程下载,原理是通过使用多个线程去争取服务器的更多资源.看下图吧
2.接着就是我们都知道文件存储是有顺序的,当存储的二进制数据0和1发生了变化的时候,文件就发生了翻天覆地的变化.所以我们必须保证下载的东西存储顺序是原来一样的,一一对应.我们的思路每个线程负责一个区域的局部下载,所以我们把请求的数据分块,并把一个文件分块(这样就不必每次都需要请求一个完整的文件了).很幸运哈,http协议有个RANGE属性字段,就是可以设置请求数据的区域范围.而且,Java也为我们提供了一个RandomAccessFile类,它可以让我们按需要去读写改某个区域.下面看图时间到...
3.下面就是分块内容了,首先我们可以获取文件的大小fileSize,然后获取每个线程应该分配的大小eachSize = fileSize / THREAD_COUNT.有了下载大小还不可以,我们还需要计算从哪里开始startIndex下载,和从从哪里结束下载endIndex(用于确定请求范围),考虑下面公式,如果有线程0 1 2.那么
0线程:
startIndex = 0 * eachSize
endIndex = 1 * eachSize - 1;
1线程:
startIndex = 1 * eachSize
endIndex = 2 * eachSize - 1;
2线程:
startIndex = 2 * eachSize
endIndex = 3 * eachSize - 1;
总结:
startIndex = i * eachSize;
endIndex = (i + 1) * eachSize - 1;
大概就能这样写?显然,最后一个线程计算的公式是有问题的,因为fileSize / THREAD_COUNT可能是有余数的,这样的话我们的下载就不完整了,那么怎么办.很简单 最后一个线程的结束就是整个文件的大小嘛...
4.那么我们下面就开始编码吧
package snippet;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MutiDownload {
private static final int THREAD_COUNT = 5;
private static final String DOWNLOAD_URL = "http://s1.music.126.net/download/osx/NeteaseMusic_1.4.3_452_web.dmg";
private static final String fileName = "/Users/August/Desktop/NeteaseMusic_1.4.3_452_web.dmg";
public static void main(String[] args) {
long fileSize;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(DOWNLOAD_URL).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
if (connection.getResponseCode() == 200) {
/**
* 当请求成功时,返回http状态码200
*/
fileSize = connection.getContentLength();
long eachSize = fileSize / THREAD_COUNT;
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
/**
* 打开一个RandomAccessFile文件,打开方式为读写(rw)
* setLength是先在存储设备占用一块空间,防止下载到一半空间不足
*/
raf.setLength(fileSize);
raf.close();
for (int i = 0; i < THREAD_COUNT; i++) {
long startIndex = i * eachSize;
long endIndex = (i + 1) * eachSize - 1;
if (i == THREAD_COUNT - 1) {
endIndex = fileSize;
}
/**
* 当时最后一个线程的时候,endIndex的值就由文件大小
*/
new DownloadThread(DOWNLOAD_URL, fileName, i, startIndex, endIndex).start();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
connection = null;
}
}
}
}
class DownloadThread extends Thread {
private String url;
private String fileName;
private int threadID;
private long startIndex;
private long endIndex;
private HttpURLConnection connection;
private RandomAccessFile raf;
private InputStream inputStream;
public DownloadThread(String url, String fileName, int threadID, long startIndex, long endIndex) {
super();
this.url = url;
this.fileName = fileName;
this.threadID = threadID;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url + "?ts=" + System.currentTimeMillis())
.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setRequestProperty("RANGE", "bytes=" + startIndex + "-" + endIndex);
/**
* 设置请求范围.
*/
/**
* 当请求部分数据成功的时候,返回http状态码206
*/
if (connection.getResponseCode() == 206) {
inputStream = connection.getInputStream();
byte[] bs = new byte[1024];
int len;
raf = new RandomAccessFile(fileName, "rwd");
raf.seek(startIndex);
/**
* 把开始写的位置设置为startIndex,与请求数据的位置一致
*/
long total = 0;
while ((len = inputStream.read(bs)) != -1) {
total += len;
System.out.println("线程" + threadID + ":" + total);
raf.write(bs, 0, len);
}
/**
* 上面是开始读取并写入数据,下面是擦屁股...
*/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.disconnect();
connection = null;
}
if (raf != null) {
raf.close();
raf = null;
}
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}