上传视频:
对文件进行分块
@Testpublic void testChunk() throws IOException {File sourceFile = new File("d:/ev/20240302_170705.mp4");String chunkPath = "d:/ev/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[] b = 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(b)) != -1) {raf_write.write(b, 0, len);if (file.length() >= chunkSize) {break;}}raf_write.close();System.out.println("完成分块"+i);}}raf_read.close();}
对文件进行合并
@Testpublic void merge() throws IOException {File sourceFile=new File("D:/ev/20240302_170705.mp4");//合并后文件的存放File mergeFile=new File("D:/ev/merge.mp4");//分块文件的存放目录File fileFloder=new File("D:/ev/chunk/");if (mergeFile.exists()){mergeFile.delete();}//创建新的合并文件boolean newFile = mergeFile.createNewFile();if (newFile){RandomAccessFile raf_w=new RandomAccessFile(mergeFile,"rw");//指向文件的顶端raf_w.seek(0);//缓冲区byte[] b=new byte[1024*4];//分块列表File[] files= fileFloder.listFiles();//转为集合List<File> fileList = Arrays.asList(files);//对分块文件按照序号进行排序Collections.sort(fileList, new Comparator<File>() {@Overridepublic int compare(File o1, File o2) {return Integer.parseInt(o1.getName())-Integer.parseInt(o2.getName());}});//合并文件for (File file : fileList) {RandomAccessFile raf_r=new RandomAccessFile(file,"r");int len=-1;while ((len=raf_r.read(b))!=-1){raf_w.write(b,0,len);}raf_r.close();}raf_w.close();}//对合并后的文件进行校验FileInputStream source=new FileInputStream(sourceFile);FileInputStream merge=new FileInputStream(mergeFile);String sou = DigestUtils.md5Hex(source);String meg = DigestUtils.md5Hex(merge);if (sou.equals(meg)){System.out.println("文件合并完成");}else{System.out.println("文件合并失败");}}
将分块文件上传到minio
/*** 测试上传分块文件*/@Testpublic void testChunk(){for (int i = 0; i < 9; i++) {try {UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder().bucket("testbucket").object("chunk/"+i).filename("D:\\ev\\chunk\\"+i).build();minioClient.uploadObject(uploadObjectArgs);System.out.println("上传成功"+i);} catch (Exception e) {System.out.println("上传失败"+i);}}}
将分块文件进行合并:
/*** 测试合并分块*/@Testpublic void test_merge() throws Exception {List<ComposeSource> sources = Stream.iterate(0, i -> ++i).limit(9).map(i -> ComposeSource.builder().bucket("testbucket").object("chunk/".concat(Integer.toString(i))).build()).collect(Collectors.toList());ComposeObjectArgs composeObjectArgs = ComposeObjectArgs.builder().bucket("testbucket").object("merge.mp4").sources(sources).build();minioClient.composeObject(composeObjectArgs);}
如何根据文件获取本地文件的路径
File tempFile=File.createTempFile("minio",".temp");//创建一个临时文件,将删除文件转存到零食文件中multipartFile.transferTo(tempFile);//文件路径String localFilePath = tempFile.getAbsolutePath();
前端对文件分块的大小为5MB,SpringBoot web默认上传文件的大小限制为1MB,这里需要在media-api工程修改配置如下:
spring: |
max-file-size:单个文件的大小限制
Max-request-size: 单次请求的大小限制