//对本地文件进行分块 @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); }
分块上传视频到minio的代码实现包含分块,上传,合并操作
于 2023-08-07 11:09:10 首次发布