后端接口
service
String uploadVideoAliy(MultipartFile file); void removeVideo(String videoId);
@Override public String uploadVideoAliy(MultipartFile file) { String videoId = null; try { //上传后显示的名称 String title = file.getOriginalFilename(); //上传文件的原始名称 String fileName = file.getOriginalFilename(); //fileName = fileName.substring(0,fileName.lastIndexOf('.')); System.out.println(fileName); InputStream inputStream = file.getInputStream(); UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream); request.setApiRegionId("cn-shanghai"); UploadVideoImpl uploader = new UploadVideoImpl(); UploadStreamResponse response = uploader.uploadStream(request); System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID if (response.isSuccess()) { System.out.print("VideoId=" + response.getVideoId() + "\n"); videoId = response.getVideoId(); } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 System.out.print("VideoId=" + response.getVideoId() + "\n"); System.out.print("ErrorCode=" + response.getCode() + "\n"); System.out.print("ErrorMessage=" + response.getMessage() + "\n"); videoId = response.getVideoId(); } }catch (Exception e){ e.printStackTrace(); } return videoId; } @Override public void removeVideo(String videoId) { //删除云端视频 try{ DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.REGION_ID,ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); DeleteVideoRequest request = new DeleteVideoRequest(); request.setVideoIds(videoId); DeleteVideoResponse response = client.getAcsResponse(request); System.out.print("RequestId = " + response.getRequestId() + "\n"); }catch (com.aliyuncs.exceptions.ClientException e){ throw new GuliException(20001, "视频删除失败"); } }
controller
@Autowired private VodService vodService; //上传视频到阿里云 @PostMapping("uploadAliyunVideo") public R uploadAliyunVideo(MultipartFile file){ String videoId = vodService.uploadVideoAliy(file); return R.ok().data("videoId",videoId); } /** * 删除阿里云视频根据videoId */ @DeleteMapping("removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable String id){ vodService.removeVideo(id); return R.ok().message("视频删除成功喽"); }