java分段式下载视频
package test;
import java.util.concurrent.CountDownLatch;
/**
* 分段式下载
*/
public class Client {
public static void main(String[] args) {
int threadSize = 10;
// String serverPath = "http://file.ws.126.net/3g/client/netease_newsreader_android.apk";
// String localPath = "NewsReader.apk";
// String serverPath = "https://www.169it.com/tech-qa-java/article-7136807303551314484.html";
String serverPath = "https://vd2.bdstatic.com/mda-mma3fxkqx9zcvcsf/1080p/cae_h264_delogo/1643243418633418856/mda-mma3fxkqx9zcvcsf.mp4?v_from_s=hkapp-haokan-tucheng&auth_key=1644479990-0-0-bd7ba1dcc808792838d7b09e56582826&bcevod_channel=searchbox_feed&pd=1&pt=3&logid=1790139036&vid=7273852985186460563&abtest=&klogid=1790139036";
String localPath = "C:\\Users\\hasee\\Desktop\\test2.mp4";
CountDownLatch latch = new CountDownLatch(threadSize);
MutiThreadDownLoad m = new MutiThreadDownLoad(threadSize, serverPath, localPath, latch);
long startTime = System.currentTimeMillis();
try {
m.executeDownLoad();
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("全部下载结束,共耗时" + (endTime - startTime) / 1000 + "s");
}
}
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import netiler.io.NetilerFileInputStream;
import netiler.util.IOUtil;
/**
* 多线程下载模型
*
* @author 分段式下载
*/
public class MutiThreadDownLoad {
/**
* 同时下载的线程数
*/
private int threadCount;
/**
* 服务器请求路径
*/
private String serverPath;
/**
* 本地路径
*/
private String localPath;
/**
* 线程计数同步辅助
*/
private CountDownLatch latch;
public MutiThreadDownLoad(int threadCount, String serverPath, String localPath, CountDownLatch latch) {
this.threadCount = threadCount;
this.serverPath = serverPath;
this.localPath = localPath;
this.latch = latch;
}
public void executeDownLoad() {
try {
URL url = new URL(serverPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
//服务器返回的数据的长度,实际上就是文件的长度,单位是字节
int length = conn.getContentLength();
System.out.println("文件总长度:" + length + "字节(B)");
RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
//指定创建的文件的长度
raf.setLength(length);
raf.close();
//分割文件
int blockSize = length / threadCount;
for (int threadId = 1; threadId <= threadCount; threadId++) {
if (threadId == 2 ) {
break;
}
//第一个线程下载的开始位置
int startIndex = (threadId - 1) * blockSize;
int endIndex = startIndex + blockSize - 1;
if (threadId == threadCount) {
//最后一个线程下载的长度稍微长一点
endIndex = length;
}
System.out.println("线程" + threadId + "下载:" + startIndex + "字节~" + endIndex + "字节");
new DownLoadThread(threadId, startIndex, endIndex).start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 内部类用于实现下载
*/
public class DownLoadThread extends Thread {
/**
* 线程ID
*/
private int threadId;
/**
* 下载起始位置
*/
private int startIndex;
/**
* 下载结束位置
*/
private int endIndex;
public DownLoadThread(int threadId, int startIndex, int endIndex) {
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
System.out.println("线程" + threadId + "正在下载...");
URL url = new URL(serverPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//请求服务器下载部分的文件的指定位置
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
conn.setConnectTimeout(50000);
int code = conn.getResponseCode();
System.out.println("线程" + threadId + "请求返回code=" + code);
InputStream is = conn.getInputStream();//返回资源
RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
//随机写文件的时候从哪个位置开始写
raf.seek(startIndex);//定位文件
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
}
is.close();
raf.close();
System.out.println("线程" + threadId + "下载完毕");
//计数值减一
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}