java大视频分片上传

实现原理,前端控制每次上传1mb,后端接受1mb,并记录该分片下标,返回给前端还未上传的下标,直到所有的都上传完成

controller

@ApiOperation(value = "上传视频", notes = "上传视频", httpMethod = "POST", response = WebResult.class)
@PostMapping(value = "/uploadVideo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//
public AjaxResult uploadVideo(@RequestParam(name = "file") MultipartFile file,
                              @RequestParam(name = "chunkIndex") Integer chunkIndex,
                              @RequestParam(name = "md5") String md5,
                              @RequestParam(name = "totalFileSize") Long totalFileSize,
                              @RequestParam(name = "fileName") String fileName,
                              @RequestParam(name = "userName") String userName,
                              HttpServletRequest request) throws Exception {
   
   
    if (null == file || file.isEmpty()) {
   
   
        return AjaxResult.error("文件内容不能为空!");
    }

    return fileSystemService.uploadVideo(file, chunkIndex, md5, fileName, userName);
    }

service

public AjaxResult uploadVideo(MultipartFile file, Integer chunkIndex, String md5, String fileName, String userName) throws IOException {
   
   
        //创建存放文件夹
        String date = DateTime.now().toString("yyyyMMdd");
        String dirPath = ConstantUtils.FILE_VIDEO_PATH + userName + "/" + date + "/" + md5 + "/";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
   
   
            dirFile.mkdirs();
        }
        //分区文件
        String relativeFilePath = dirPath + fileName;
        File tempFile = new File(relativeFilePath);
        RandomAccessFile rw = new RandomAccessFile(tempFile, "rw");
        //定位到分片的偏移量
        rw.seek(Long.parseLong(ConstantUtils.FILE_VIDEO_CHUNK_SIZE) * chunkIndex);
        //写入分片数据
        rw.write(file.getBytes());
        //关闭流
        rw.close();
        //读取已经分片的集合
        Set<Object> hasChunkList = new HashSet<>();
        String hasChunkKey = ConstantUtils.CHUNK_PREFIX + md5;
        if (redisHelper.hasKey(hasChunkKey
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值