实现原理,前端控制每次上传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

最低0.47元/天 解锁文章
1130

被折叠的 条评论
为什么被折叠?



