其他分享
首页 > 其他分享> > 利用阿里云的视频点播上传视频的步骤

利用阿里云的视频点播上传视频的步骤

作者:互联网

1、编写控制层controller,cotroller只需要返回视频的id,利用MultipartFile接收前端的文件

@RestController
@CrossOrigin
@RequestMapping("/eduvod/video")
public class VodController {
    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("/uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file){
        //返回上传视频的id值
        String videoId = vodService.uploadVideoAly(file);

        return R.ok().data("videoId",videoId);
    }
}

2、编写业务层,参考阿里云里面的文件上传的方法

@Service
public class VodServiceImpl implements VodService {
    //上传视频到阿里云
    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
//        accessKeyId,accessKeySecret,

//        fileName:上传文件原始名称
            String fileName = file.getOriginalFilename();
//        title:上传之后显示的名称
            String title = fileName.substring(0,fileName.lastIndexOf("."));
//        inputStream:上传文件输入流
            InputStream inputStream = file.getInputStream();
            //
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);


            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);

            String videoId = null;
            if(response.isSuccess()){
                videoId = response.getVideoId();
            }else{
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

3、设置配置文件上传的大小

# 服务器端口
server.port=8003

#服务名
spring.application.name=service-vod

#环境设置:dev、test、prod
spring.profiles.active=dev

#设置上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB

#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=LTAI5tH3WSRcqaHYumqdLPmD
aliyun.vod.file.keysecret=WCCdg5FijqmaynW3zvRhfsy2I0rd6A

4、编写工具类,用于存放账户密码,实现InitialzionBean接口,重写里面的方法,这个方法里面的东西是在赋值之后执行的方法

@Component
public class ConstantVodUtils implements InitializingBean {

    @Value("${aliyun.vod.file.keyid}")
    private String keyId;

    @Value("${aliyun.vod.file.keysecret}")
    private String keySecret;

    public static String ACCESS_KEY_SECRET;
    public static String ACCESS_KEY_ID;
    @Override
    public void afterPropertiesSet() throws Exception {
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
    }
}

标签:String,videoId,阿里,KEY,file,视频点播,上传,public
来源: https://www.cnblogs.com/joxgod/p/15643143.html