分块上传视频到minio的代码实现包含分块,上传,合并操作

//对本地文件进行分块
@Test
public void testChunk() throws IOException {
    File sourceFile = new File("G:\\hu.mp4");
    String chunkPath = "G:\\chunk\\";
    File chunkFolder = new File(chunkPath);
    if(!chunkFolder.exists()){
        chunkFolder.mkdirs();
    }
    //分块大小
    long chunkSize = 1024*1024*5;
    //分块数量
    long chunkNum = (long) Math.ceil(sourceFile.length() * 1.0 / chunkSize);
    System.out.println("分块总数为:"+chunkNum);
    //缓冲区大下
    byte[] bytes = new byte[1024];
    //使用RandomAccessFile访问文件
    RandomAccessFile raf_read = new RandomAccessFile(sourceFile, "r");
    for(int i = 0 ;i<chunkNum;i++){
        File file = new File(chunkPath + i);
        if(file.exists()){
            file.delete();
        }
        boolean newFile = file.createNewFile();
        if(newFile){
            //向分块文件写
            RandomAccessFile raf_write = new RandomAccessFile(file, "rw");
            int len = -1;
            while((len = raf_read.read(bytes))!=-1){
                raf_write.write(bytes,0,len);
                if(file.length()>=chunkSize){
                    break;
                }
            }
            raf_write.close();
            System.out.println("完成分块 "+i);
        }
    }
    raf_read.close();


}
//在本地合并分块
@Test
public void testMerge() throws IOException{
    File sourceFile = new File("G:\\hu.mp4");
    String chunkPath = "G:\\chunk\\";
    File chunkFolder = new File(chunkPath);
    File mergeFile = new File("G:\\hu1.mp4");
    if(mergeFile.exists()){
        mergeFile.createNewFile();
    }
    mergeFile.createNewFile();
    RandomAccessFile raf_write = new RandomAccessFile(mergeFile, "rw");
    //指针指向文件顶端
    raf_write.seek(0);
    byte[] bytes = new byte[1024];
    File[] fileArray = chunkFolder.listFiles();//因为我们货区文件的顺序和我们系统的文件系统有关系,因此可能不是按照文件顺序进行排列的因此要排序
    List<File> fileList = Arrays.asList(fileArray);
    Collections.sort(fileList, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            return Integer.parseInt(o1.getName())-Integer.parseInt(o2.getName());

        }
    });
    for(File file:fileList){
        RandomAccessFile raf_read = new RandomAccessFile(file, "r");
        int len = -1;
        while((len = raf_read.read(bytes))!=-1){
            raf_write.write(bytes,0,len);
        }
        raf_read.close();
    }
    raf_write.close();
    //校验文件
    FileInputStream fileInputStream = new FileInputStream(sourceFile);
    FileInputStream mergeInputStream = new FileInputStream(mergeFile);
    String originMd5 = DigestUtils.md5Hex(fileInputStream);
    String mergeMd5 = DigestUtils.md5Hex(mergeInputStream);
    if(originMd5.equals(mergeMd5)){
        System.out.println("合并文件成功");
    }else{
        System.out.println("合并文件失败");
    }

//上传分块到minio

@Test
public void upLoadChunk() throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    for (int i = 0; i < 3; i++) {
        UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                .bucket("testbucket")
                .filename("G:\\chunk\\" + i)
                .object("chunk/" + i)
                .build();
        minioClient.uploadObject(uploadObjectArgs);
        System.out.println("上传文件成功");
    }
}
//调用minio接口合并分块 合并的分块在minio服务器上
public void testMerge() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    /*ArrayList<ComposeSource> source = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ComposeSource composeSource = ComposeSource.builder().bucket("testbucket").object("chunk/" + i).build();
        source.add(composeSource);
    }*/
    //使用stream流进行优化
    List<ComposeSource> sources = Stream.iterate(0, i -> i++).limit(3).map(i -> ComposeSource.builder().bucket("testbucket").object("chunk/" + i).build()).collect(Collectors.toList());

    ComposeObjectArgs composeObjectArgs = ComposeObjectArgs.builder()
            .bucket("testbucket")
            .object("merge.mp4")
            .sources(sources)
            .build();
    minioClient.composeObject(composeObjectArgs);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值