import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoad {
// 声明下载路径“红色警戒2”
public static final String PATH = "http://soft3.xzstatic.com/2015/10/hsjj2ghgzh.rar";
public static int threadCount = 0;// 声明线程数量
public static void main(String[] args) {
try {
URL url = new URL(PATH);
// 获取连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 通过获取连接定义文件名
String[] str = PATH.split("/");
String fileName = str[5];
// 获取下载文件大小
int fileLength = conn.getContentLength();
System.out.println(fileName);
// 在本地创建一个与服务器大小一致的可随机写入文件
RandomAccessFile raf = new RandomAccessFile(fileName, "rwd");
System.out.println(fileLength);// 测试用
raf.setLength(fileLength);
// 自定义线程数量
threadCount = 3;
// 计算每条线程下载数据的大小
int blockSize = fileLength / threadCount;
// 启动线程下载
for (int threadId = 1; threadId <= threadCount; threadId++) {
// 核心代码,定义每个线程开始以及结束的下载位置
int startPos = (threadId - 1) * blockSize;// 开始下载的位置
int endPos = (threadId * blockSize) - 1;// 结束下载的位置(不包含最后一块)
if (threadCount == threadId) {
endPos = fileLength;
}
new Thread(new DownLoadThread(threadId, startPos, endPos, PATH))
.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 实现下载线程
static class DownLoadThread implements Runnable {
private int threadId;
private int startPos;
private int endPos;
private String path;
public DownLoadThread(int threadId, int startPos, int endPos,
String path) {
super();
this.threadId = threadId;
this.startPos = startPos;
this.endPos = endPos;
this.path = path;
}
public void run() {
try {
URL url = new URL(path);
String[] str = PATH.split("/");
String fileName = str[5];
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 设置URL请求的方法(具体参考API)
conn.setRequestMethod("GET");
// 设置500毫秒为超时值
conn.setReadTimeout(5000);
File file = new File(threadId + ".txt");
if (file.exists() && file.length() > 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
String saveStartPos = br.readLine();
if (saveStartPos != null && saveStartPos.length() > 0) {
startPos = Integer.parseInt(saveStartPos);
}
}
// 注意双引号内的格式,不能包含空格(等其他字符),否则报416
conn.setRequestProperty("Range", "bytes=" + startPos + "-"
+ endPos);
RandomAccessFile raf = new RandomAccessFile(fileName, "rwd");// 存储下载文件的随机写入文件
raf.seek(startPos);// 设置开始下载的位置
System.out.println("线程" + threadId + ":" + startPos + "~~"
+ endPos);
InputStream is = conn.getInputStream();
byte[] b = new byte[1024 * 1024 * 10];
int len = -1;
int newPos = startPos;
while ((len = is.read(b)) != -1) {
RandomAccessFile rr = new RandomAccessFile(file, "rwd");// 存储下载标记的文件
raf.write(b, 0, len);
// 将下载标记存入指定文档
String savaPoint = String.valueOf(newPos += len);
rr.write(savaPoint.getBytes());
rr.close();
}
is.close();
raf.close();
System.out.println("下载完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有