多线程下载迅雷
package hp.DownLoad;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownLoadThread extends Thread {
private int stratIndex;//开始下标
private int endIndex;//结束下标
private int threadid;//线程编号
//构造函数 初始化成员变量
public DownLoadThread(int stratIndex, int endIndex, int threadid) {
this.stratIndex = stratIndex;
this.endIndex = endIndex;
this.threadid = threadid;
}
//
public void run() {
try {
URL url = new URL(DownLoadTest.path);//接收网络下载的路径
//根据接收到的路径打开网络连接 由于连接是http请求的 所以要返回HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求网络时会用到的参数
/**
* setConnectTimeout 连接超时 (单位:毫秒)
* setReadTimeout 读取超时 (单位:毫秒)
* setRequestMethod 请求方式 post/get
* */
conn.setConnectTimeout(5000);// 连接超过五秒算连接超时
conn.setReadTimeout(5000); // 读取数据时间超过五秒算超时
conn.setRequestMethod("GET");// 设置请求方式为get请求
//getResponseCode() 状态码
/**
*
服务器常见响应码
500: 服务器内部错误
404: 找不到路径
200: 成功
206: 也是成功 ,是部分数据获取成功
*
* */
if (conn.getResponseCode() == 206) {
//只要有连接成功 conn里就已经包含了服务器返回的数据
InputStream inp = conn.getInputStream();
//该文件用于 存放 写入的数据 (可以理解为 临时文件 ,它和被下载文件大小一致 )
File file = new File("BaiduNetdisk_6.9.7.4.exe");
//RandomAccessFile 支持随机读写
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(stratIndex); //跳到要读取位置 从起始下标开始读取
System.out.println("线程:" + threadid + "下载开头位置:" + stratIndex + "---" + "下载结束位置:" + endIndex);
//开始拷贝
byte[] buf = new byte[1024];//一兆一兆的读取
int len = 0;
while ((len = inp.read(buf)) != -1) {//当接收到的inp=0(即从头开始)!=-1(即没读到文件尾)时 写入
raf.write(buf, 0, len);
}
//
System.out.println("线程:" + threadid + " 下载完毕了...");
raf.close();//当前线程结束 停止(关闭)读取
DownLoadTest.finishedThead++;//下载完的线程++
//解决多线程并发
synchronized (DownLoadTest.path) {
DownLoadTest.finishedThead = 0;//当每个线程都下载完了 将下载完的线程归零
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
写测试类 完成下载
package hp.DownLoad;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DownLoadTest {
//具体下载路径
static String path = "http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";
static int finishedThead = 0;//下载完成的线程
static int threadCount = 3;//有三个线程
public static void main(String[] args) throws IOException {
URL url = new URL(DownLoadTest.path);//接受网络下载路径
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
//如果完全链接成功
if(conn.getResponseCode()==200){
int contentLength = conn.getContentLength();//获取到文件总长
int size = contentLength/threadCount; //每个线程下载的大小
//开始循环下载
for (int i = 0; i < threadCount; i++) {
int startIndex = i*size; //线程开头的位置
int endIndex = (i+1)*size; //线程结束的位置
DownLoadThread td = new DownLoadThread(startIndex,endIndex,i);
td.start(); //开启线程
}
}
}
}