断点续传_大文件分块测试

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();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值