您的位置:首页 > 其它

多线程下载资源

2015-12-30 17:48 239 查看
多线程下载服务器端资源思路如下:

1,在android 客户端首先创建一个空文件,空白文件大小和服务器文件大小一样。

2,开启若干线程分别去下载服务器端对应的资源。

3. 如果所有的线程都下载完毕,服务器端资源就被下载完毕。

我们遇到的问题:

a,如何划分服务器端资源。

b, 每个线程资源的开始和结束位置。

1》获取服务器端资源文件的大小。

long size = conn.getContentLength();


2》根据开启线程的数量,把服务器的资源等分为若干份。

size: 表示服务器端资源的大小

threadCount:表示要开启的线程数量

blocksize = size /threadCount; 表示每个线程要下载资源的大小。

for (int i = 1; i <= threadCount; i++) {
System.out.println("开启线程" + i);
long startIndex = (i - 1) * blocksize;
long endIndex = i * blocksize-1;

// 最后一个线程
if (i == threadCount) {
endIndex = size-1;
}

System.out.println("开启线程" + i + "下载的位置" + startIndex + "~~" + endIndex);

}


比如开启3个线程,服务器资源大小为10.
第一个线程下载的资源:0~~2
第二个线程要下载的资源:3~~5
第三个线程要下载的资源:6~~9

完整代码如下:

package com.zhq.demo;

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

public class TestDownload {
// 线程数量
private static int threadCount = 3;

// 每个区块的大小
private static Long blocksize;

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

String path = "http://localhost:8080/DownLoadTest/sqllite.dmg";

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();

if (code == 200) {

long size = conn.getContentLength();

System.out.println("size==" + size);

blocksize = size / threadCount;

// 1. 本地创建一个大小跟服务器一模一样的空白文件。
File file = new File("temp.dmg");
RandomAccessFile raf1 = new RandomAccessFile(file, "rw");
raf1.setLength(size);
// 2. 开启若干个自线程分别去下载对应的资源。

for (int i = 1; i <= threadCount; i++) {
System.out.println("开启线程" + i);
long startIndex = (i - 1) * blocksize;
long endIndex = i * blocksize-1;

// 最后一个线程
if (i == threadCount) {
endIndex = size-1;
}

System.out.println("开启线程" + i + "下载的位置" + startIndex + "~~" + endIndex);

new DownLoadThread(path, i, startIndex, endIndex).start();
}

}
conn.disconnect();

}

private static class DownLoadThread extends Thread {
private int threadId;
private long startIndex;
private long endIndex;
private String path;

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

@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.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("temp.dmg");
RandomAccessFile raf=new RandomAccessFile(file, "rw");
raf.seek(startIndex);
System.out.println("第"+threadId+"个线程:写文件的开始位置"+String.valueOf(startIndex));
int len=0;
byte[]buffer =new byte[1024];
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);

}
is.close();
raf.close();
System.out.println("线程"+threadId+"下载完毕了");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

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