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;
public class DownLoad {
//定义线程数
static int threadCount = 3;
//下载地址
static String path = "http://127.0.0.1/test.avi";
public static void main(String[] args) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
//获取文件总的字节数
int len = conn.getContentLength();
//计算每个线程下载的字节数
int size = len / threadCount;
//指定临时文件的路径和文件名
File file = new File(getFileName(path));
RandomAccessFile raf = new RandomAccessFile(file, "rw");
//设置临时文件的大小,跟服务器文件一模一样
raf.setLength(len);
for (int i = 0; i < threadCount; i++) {
//开始位置
int startIndex = i * size;
//结束位置
int endIndex = (i + 1) * size - 1;
//设置最后一个线程的结束位置为总的字节数减去1
if (i == threadCount - 1) {
endIndex = len - 1;
}
System.out.println("线程" + i + "," + startIndex + "--->" + endIndex);
//启动线程开始下载
new MyThread(i, size, startIndex, endIndex).start();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getFileName(String path) {
int index = path.lastIndexOf("/");
return path.substring(index + 1);
}
static class MyThread extends Thread {
@Override
public void run() {
try {
int total = startIndex;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
//设置请求的数据的范围bytes=10-100表示请求第10到100的字节
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
//这里的请求206为成功
if (conn.getResponseCode() == 206) {
InputStream is = conn.getInputStream();
File file = new File(getFileName(path));
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(startIndex);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
total +=len;
System.out.println("线程" + threadNum + "----->已下载" + total);
raf.write(b, 0, len);
}
raf.close();
System.out.println("线程" + threadNum + "下载完成");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int size;
int startIndex;
int endIndex;
int threadNum;
public MyThread(int threadNum, int size, int startIndex, int endIndex) {
super();
this.size = size;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadNum = threadNum;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getEndIndex() {
return endIndex;
}
public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}
public MyThread(int size, int startIndex, int endIndex) {
this.size = size;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
public MyThread() {
}
}
}
多线程下载
最新推荐文章于 2025-12-05 17:02:52 发布
1715

被折叠的 条评论
为什么被折叠?



