spring boot实现阿里云视频点播上传视频功能(复制粘贴即可)
目录:
1.spring boot实现阿里云视频点播上传视频(复制粘贴即可)
2.spring boot 实现阿里云视频点播 --删除视频
准备工作:
阿里云地址: https://www.aliyun.com/
登录阿里云,确保账户有余额,有几毛钱就够了,开发学习基本不花钱。
2.进入阿里云开通视频点播功能
3.获取阿里云密钥
代码编写:
目录结构:
application.properties
# 服务端口server.port=8003# 服务名spring.application.name=vod# 环境设置:dev、test、prodspring.profiles.active=dev#阿里云 vod#不同的服务器,地址不同aliyun.vod.file.keyid=自己的密钥idaliyun.vod.file.keysecret=自己的密钥# 最大上传单个文件大小:默认1Mspring.servlet.multipart.max-file-size=1024MB# 最大置总上传的数据大小 :默认10Mspring.servlet.multipart.max-request-size=1024MB
ConstantVodUtils ,获取配置文件的配置信息
@Componentpublic 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; }}
VodController
@RestController@RequestMapping('/vod/video')@CrossOriginpublic class VodController { @Autowired private VodService vodService; //上传视频到阿里云 @PostMapping('uploadAlyiVideo') public String uploadAlyiVideo(MultipartFile file) { //返回上传视频id String videoId = vodService.uploadVideoAly(file); return videoId; }}
service
public interface VodService { //上传视频到阿里云 String uploadVideoAly(MultipartFile file);}
@Servicepublic 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 uploader = new UploadVideoImpl(); UploadStreamResponse response = uploader.uploadStream(request); String videoId = null; if (response.isSuccess()) { videoId = response.getVideoId(); } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 videoId = response.getVideoId(); } return videoId; }catch(Exception e) { e.printStackTrace(); return null; } }}
界面简单编写
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form method='post' enctype='multipart/form-data' action='http://127.0.0.1:8003/vod/video/uploadAlyiVideo'> <input type='file' name='file'/> <input type='submit' value='提交'/></form></body></html>
上传成功后返回视频id
且在阿里云点播控制台看到视频已经上传
到此这篇关于spring boot实现阿里云视频点播上传视频(复制粘贴即可)的文章就介绍到这了,更多相关spring boot阿里云视频点播上传视频内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: