多线程下载文件
package cn.hp;
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;
/**
* 多线程下载
* @author wyl
* 通过URL和下载资源建立联系
* 设置参数,读取的范围
* 开始进行文件的读取和写入
* 关闭流
*
*/
public class DownLoad2 extends Thread {
//多线程下载需要的信息
private long startIndex;
private long endIndex;
private int threadId;
private String name;
private static String path="D:\\upload\\";
static {
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
}
public DownLoad2(long startIndex, long endIndex, int threadId,String name) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
this.name=name;
}
@Override
public void run() {
long start =System.currentTimeMillis();
try {
//统一资源管理器,可以直接打开网络地址
URL url = new URL(DownLoadTest2.path);//获取资源地址
//因为是基于Http请求,获取httpUrlConnection,获取网络连接对象,和服务器建立联系
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置参数
conn.setRequestMethod("GET");
conn.setConnectTimeout(50000);//连接超时
conn.setReadTimeout(100000);//读取超时
conn.addRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);//设置读取范围
if(conn.getResponseCode()==206) {//部分数据ok
//取出连接中的数据
InputStream is = conn.getInputStream();
//提供一个接收的地方(本地接收)
File file = new File(path+name);
//输出流,开始写数据,随机读写流
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(startIndex);//跳转到线程开始的位置
System.out.println("线程:"+this.getName()+" "+threadId+" 开始的位置:"+startIndex+"---"+" 结束位置"+endIndex);
int i=0;
byte [] buf= new byte[8192];//字节数组,提高读取速度
int len=0;//字节数组读取的长度
while((len=is.read(buf))!=-1) {
System.out.println(this.getName()+" "+(i++));
raf.write(buf, 0, len);
}
raf.close();
is.close();
DownLoadTest2.okThread++;
synchronized (DownLoadTest2.path) {
System.out.println("线程:"+this.getName()+threadId+"下载完毕... "+DownLoadTest2.okThread);
if(DownLoadTest2.okThread==DownLoadTest2.countThread) {
DownLoadTest2.okThread=0;
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end =System.currentTimeMillis();
System.out.println("线程:"+this.getName()+threadId+"运行时间"+(end - start));
}
}
package cn.hp;
import com.entity.DownLoad2;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 多线程下载测试类
*/
public class DownLoadTest2 {
static String path = "https://plugins.jetbrains.com/files/14662/92372/gitee-idea-plugin-1.4-SNAPSHOT.zip?updateId=92372&pluginId=14662&family=INTELLIJ";
static int countThread=2;//线程数
static int okThread=0;//和线程数有关,记录线程完成的个数
public static void main(String[] args) throws Exception {
long start =System.currentTimeMillis();
//统一资源管理器,可以直接打开网络地址
URL url = new URL(DownLoadTest2.path);
//因为是基于Http请求,获取httpUrlConnection,获取网络连接对象,和服务器建立联系
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置参数
conn.setRequestMethod("GET");
conn.setConnectTimeout(50000);//连接超时
conn.setReadTimeout(200000);//读取超时
//判断资源链接是否建立成功,成功进行多线程下载分配
if(conn.getResponseCode()==200) {//成功
//获取文件总长度
long count = conn.getContentLengthLong();
//每个线程下载的大小
long size = count/countThread;
//开启线程进行下载
for (int i = 0; i < 1; i++) {
long startIndex=size*i;//开始位置 0 102.4
long endIndex=size*(i+1);//结束位置 102.4 204.8
DownLoad2 thread = new DownLoad2(startIndex,endIndex , i,"gitee-idea-plugin-1.4-SNAPSHOT.zip");
thread.start();
}
}
long end =System.currentTimeMillis();
System.out.println(end-start);
}
}