您的位置:首页 > 编程语言 > Java开发

多线程下载的原理(1) java事例

2015-12-20 18:08 363 查看
先打开电脑上的Tomcat,在Tomcat的bin目录下。

然后再在Tomcat的webapps/root目录下放一个.exe文件,来试验多线程下载。

创建一个java工程:



Demo代码:

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

public class Demo {
public static int threadCount=3;
public static void main(String[] args) throws Exception {
//1.连接服务器,获取一个文件,获取稳健的长度,在本地创建一个大小跟服务器文件一样的临时文件。
String path="http://192.168.0.14:8080/Yodao.exe";
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if(code==200){
//服务器返回的数据的长度,实际上就是文件的长度
int length =conn.getContentLength();
System.out.println("文件的总长度"+length);
//在客户端本地创建一个大小跟服务器端文件一样大小的临时文件
RandomAccessFile raf=new RandomAccessFile("setup.exe", "rwd");
//指定创建的这个文件的长度
raf.setLength(length);
raf.close();

//假设3个线程
//平均每一个线程下载的文件的大小
int blockSize=length/threadCount;

for(int threadId=1;threadId<=threadCount;threadId++){
int startIndex=(threadId-1)*blockSize;
int endIndex=threadId*blockSize-1;
if(threadId==threadCount){
//最后一个线程下载的长度要稍微长一点
endIndex=length;
}
System.out.println("线程"+threadId+"下载:---"+startIndex+"--->"+endIndex);
new DownloadThread(path, threadId, startIndex, endIndex).start();
}

}else{
System.out.println("服务器错误");
}

}
//下载文件的子线程,每一个线程下载对应位置的文件
public static class DownloadThread extends Thread{
private int threadId;//线程id
private int startIndex;//线程下载的开始位置
private int endIndex;//线程下载的结束位置
private String path;//下载文件在服务器上的路经

public DownloadThread(String path,int threadId,int startIndex,int endIndex){
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();//服务器请求全部资源200 ok;如果服务器请求部分资源206 ok.
System.out.println("code"+code);
InputStream is=conn.getInputStream();//已经设置了请求的位置,返回的是当前位置对应的文件的输入流
RandomAccessFile raf=new RandomAccessFile("setup.exe", "rwd");
raf.seek(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) {

e.printStackTrace();
}
}

}
}


打印出来的结果:



然后在项目中刷新 ,出现一个文件,和之前存的文件一样。

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