1.添加依赖
重点:aliyun-sdk-vod-upload依赖未开源,需要下载该依赖,再手动添加到maven仓库中。
2.文件配置
配置文件中加入vod的配置.
3.具体实现
配置类:
# ??? server.port=8004 # ??? spring.application.name=service-vod # ?? spring.profiles.active=dev #???vod?? aliyun.vod.file.keyid=你的keyid aliyun.vod.file.keysecret=你的keysecret # ???????????????1M spring.servlet.multipart.max-file-size=1024MB # ?????????????10M spring.servlet.multipart.max-request-size=1024MB
常量类:
@Component public class ConstantVodVideo implements InitializingBean { @Value("${aliyun.vod.file.keyid}") private String keyId; @Value("${aliyun.vod.file.keysecret}") private String keySecret; public static String ACCESS_KEY_ID; public static String ACCESS_KEY_SECRET; @Override public void afterPropertiesSet() throws Exception { ACCESS_KEY_ID=keyId; ACCESS_KEY_SECRET=keySecret; }
实现:
public class VideoServiceImpl implements VideoService { //阿里云视频上传 @Override public String uploadVideo(MultipartFile file) { try { //fileName上传文件原始名称 String fileName=file.getOriginalFilename(); //title,上传之后显示的名称 String title=fileName.substring(0,fileName.lastIndexOf("."));//substring截取fileName名称的一部分 //input,上传文件输入流 InputStream inputStream=file.getInputStream(); UploadStreamRequest request=new UploadStreamRequest(ConstantVodVideo.ACCESS_KEY_ID,ConstantVodVideo.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 (Exception e) { e.printStackTrace(); return null; } }