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

JAVA实现多线程下载

2013-05-26 11:24 295 查看
JAVA多线程下载,多线程下载是为了更多的抢占服务器的资源,以达到最快的下载速度,但是,不建议开启过多线程,因为每台PC的CPU执行效率都不一样,如果开启线程较多,反而会下载的非常慢。

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

public class MulThreadDownload {
private long startTime;
public static void main(String[] args) throws Exception {
System.out.println("开始下载");
String path="http://192.168.1.120:8080/android-studio-bundle-130.677228-windows.exe";
new MulThreadDownload().download(path,2);

}
/**
* 下载文件
* @param path 网络文件路径
* @param threadsize 线程数,不建议开启过多线程。
* @throws Exception
*/
private void download(String path,int threadsize) throws Exception {
startTime=new Date().getTime();//获取文件开始现在时间
System.out.println("开始时间:"+new Date());
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
int length=conn.getContentLength();//获取网络文件长度
File file=new File(getFileName(path));//如果不写路径则默认存放在项目的根目录下
RandomAccessFile accessFile=new RandomAccessFile(file, "rwd");//在本地生成一个长度相等的文件
accessFile.setLength(length);//把文件的长度设置的和网络上文件长度相等
accessFile.close();
//计算每条线程负责下载的数据量
int block=length%threadsize==0 ? length/threadsize :length/threadsize+1;
for (int threadid = 0; threadid < threadsize; threadid++) {
System.out.println("第"+(threadid+1)+"线程下载开始");
new DownloadThread(threadid,block,url,file).start();
}
}else{
System.out.println("下载失败");
}
}
/**
* 线程类,负责下载操作
*/
private class DownloadThread extends Thread{
private int threadid;//线程ID
private int block;//每条线程负责下载的数据量
private URL url;//线程下载路径
private File file;//线程下载文件
/**
* 下载线程
* @param threadid 线程ID
* @param block 每条线程负责下载的数据量
* @param url 线程下载的路径
* @param file 线程下载的文件
*/
public DownloadThread(int threadid, int block, URL url, File file) {
this.threadid=threadid;
this.block=block;
this.url=url;
this.file=file;
}
@Override
public void run() {
int start=threadid*block;//计算该线程从网络文件的什么位置开始下载
int end=(threadid+1)*block-1;//下载到网络文件的什么位置结束
try{
RandomAccessFile accessFile=new RandomAccessFile(file, "rwd");//在本地生成一个长度相等的文件
accessFile.seek(start);//移动指针到文件的某个位置
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Range","bytes="+start+"-"+end);//指定从网络上哪个区域开始下载数据
//注意,分段下载的网络请求返回编码是206而不是200
if(conn.getResponseCode()==206){
InputStream inStream=conn.getInputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inStream.read(buffer))!=-1){
accessFile.write(buffer, 0, len);
}
accessFile.close();
inStream.close();
}
System.out.println("第"+(threadid+1)+"线程已经下载完成");
if((threadid+1)==3){//如果线程下载ID等于最大线程数,说明下载完成,并且获取当前时间统计下载的总时间
System.out.println("结束时间:"+new Date());
long conutTime=new Date().getTime()-startTime;
System.out.println("下载总耗用时间(毫秒):"+conutTime);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 取得文件名称
* @param path
* @return
*/
private String getFileName(String path) {
return path.substring(path.lastIndexOf("/")+1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: