import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* ClassName: Test_201
* Package: com.srr.media.minio
* Description: 大文件分块操作
*
* @Author srr
* @Create 2023/3/20 23:19
* @Version 1.0
*/
public class Test_201 {
public static void main(String[] args) throws IOException {
//1. 源文件
File sourceFile = new File("C:\\Users\\Administrator\\Desktop\\尚硅谷NodeJS核心基础\\视频\\200_完结篇.mp4");
//2. 分块的文件夹的位置
String chunkPath = "C:\\Users\\Administrator\\Desktop\\chunk\\";
File chunkFolder = new File(chunkPath);
//3. 如果文件夹不存在则创建
if (!chunkFolder.exists()) {
chunkFolder.mkdirs();
}
//4. 分块的大小
long chunkSize = 1024 * 1024 * 1;
//5. 分块的数量
long chunkNum = (long) Math.ceil(sourceFile.length() * 1.0 / chunkSize);
System.out.println("分块总数::" + chunkNum);
//6. 创建随机文件读取流
RandomAccessFile raf_read = new RandomAccessFile(sourceFile, "r");
byte[] buffer = new byte[1024];
//7. 循环把文件读到文块中
for (int i = 0; i < chunkNum; i++) {
//8. 创建分块文件
File file = new File(chunkPath + i);
//9 文件文件已经存在则删除
if (file.exists()) file.delete();
//10 创建分块文件成功则开始读和写
if (file.createNewFile()) {
RandomAccessFile raf_write = new RandomAccessFile(file, "rw");
int len = -1;
while ((len = raf_read.read(buffer)) != -1) {
raf_write.write(buffer, 0, len);
//11.分块文件的长度大于分块的大小 则停止循环读写
if (file.length() >= chunkSize) {
break;
}
}
raf_write.close();
System.out.println("完成分块::" + i);
}
}
raf_read.close();
}
}
断点续传_大文件分块测试
最新推荐文章于 2024-01-23 16:31:03 发布