标签:upload html googl imp stc beans ota hup ons
官方SDK文档地址: https://help.aliyun.com/document_detail/57756.html?spm=a2c4g.11186623.6.904.4e0d3bd9VbkICO
1、引入maven依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.3.3</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.5</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
2、application.yml配置
#阿里云accessKeyId video_accessKeyId: LTAI #阿里云accessKeySecret video_accessKeySecret: kKUciySv0 #阿里云账号ID 值的来源https://help.aliyun.com/knowledge_detail/37196.html video_userId: 103907
3、控制器类
AliyunVideoController.java
(RequestUtils为获取参数的工具类,可以不使用这种方式)
RequestUtils地址:https://www.cnblogs.com/pxblog/p/12238509.html
package cn.controller; import cn.RequestUtils; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.vod.model.v20170321.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; @Controller public class AliyunVideoController { @Value("${video_accessKeyId}") private String accessKeyId; @Value("${video_accessKeySecret}") private String accessKeySecret; @Value("${video_userId}") private String videoUserId; /** * 获取视频上传地址和凭证 * */ @RequestMapping("/aliyunVideo/createUploadVideo") @ResponseBody public void createUploadVideo(HttpServletRequest request, HttpServletResponse httpServletResponse) { DefaultAcsClient client = initVodClient(); CreateUploadVideoResponse response = new CreateUploadVideoResponse(); String title= RequestUtils.getQueryParam(request,"title"); //视频标题 String fileName= RequestUtils.getQueryParam(request,"fileName"); //文件名称 try { response = createUploadVideo(client,title,fileName); System.out.print("VideoId = " + response.getVideoId() + "\n"); System.out.print("UploadAddress = " + response.getUploadAddress() + "\n"); System.out.print("UploadAuth = " + response.getUploadAuth() + "\n"); } catch (Exception e) { System.out.print("ErrorMessage = " + e.getLocalizedMessage()); } System.out.print("RequestId = " + response.getRequestId() + "\n"); } /** * 获取视频上传地址和凭证 * * @param client 发送请求客户端 * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据 * @throws Exception */ public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client,String title,String fileName) { try { CreateUploadVideoRequest request = new CreateUploadVideoRequest(); request.setTitle(title); request.setFileName(fileName); return client.getAcsResponse(request); } catch (ClientException e) { e.printStackTrace(); } return null; } /** * 刷新视频上传凭证 * */ @RequestMapping(value = "/aliyunVideo/refreshUploadVideo") @ResponseBody public void refreshUploadVideo(HttpServletRequest request){ String videoId=RequestUtils.getQueryParam(request,"videoId"); DefaultAcsClient client = initVodClient(); RefreshUploadVideoResponse response = new RefreshUploadVideoResponse(); try { response = refreshUploadVideo(client,videoId); System.out.print("UploadAddress = " + response.getUploadAddress() + "\n"); System.out.print("UploadAuth = " + response.getUploadAuth() + "\n"); } catch (Exception e) { System.out.print("ErrorMessage = " + e.getLocalizedMessage()); } System.out.print("RequestId = " + response.getRequestId() + "\n"); } /** * 刷新视频上传凭证 * * @param client 发送请求客户端 * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据 * @throws Exception */ public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client,String videoId) throws Exception { RefreshUploadVideoRequest request = new RefreshUploadVideoRequest(); request.setVideoId(videoId); return client.getAcsResponse(request); } /** * 获取播放地址函数 * 直接获取播放地址,可以使用任意播放器直接播放 * @param request */ @RequestMapping(value = "/aliyunVideo/getPlayInfo") @ResponseBody public void getPlayInfo(HttpServletRequest request){ String videoId=RequestUtils.getQueryParam(request,"videoId"); DefaultAcsClient client = initVodClient(); GetPlayInfoResponse response = new GetPlayInfoResponse(); try { response = getPlayInfo(client,videoId); List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList(); //播放地址 for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) { System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n"); } //Base信息 System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n"); } catch (Exception e) { System.out.print("ErrorMessage = " + e.getLocalizedMessage()); } System.out.print("RequestId = " + response.getRequestId() + "\n"); } /*获取播放地址函数*/ public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client,String videoId) throws Exception { GetPlayInfoRequest request = new GetPlayInfoRequest(); request.setVideoId(videoId); return client.getAcsResponse(request); } /** * 获取播放凭证函数 * 必须使用阿里云播放器进行播放 * @param request */ @RequestMapping(value = "/aliyunVideo/getVideoPlayAuth") public void getVideoPlayAuth(HttpServletRequest request){ String videoId=RequestUtils.getQueryParam(request,"videoId"); DefaultAcsClient client = initVodClient(); GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse(); try { response = getVideoPlayAuth(client,videoId); //播放凭证 System.out.print("PlayAuth = " + response.getPlayAuth() + "\n"); //VideoMeta信息 System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n"); } catch (Exception e) { System.out.print("ErrorMessage = " + e.getLocalizedMessage()); } System.out.print("RequestId = " + response.getRequestId() + "\n"); } /*获取播放凭证函数*/ public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client,String videoId) throws Exception { GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); request.setVideoId(videoId); return client.getAcsResponse(request); } @PostConstruct public DefaultAcsClient initVodClient() { try { String regionId = "cn-shanghai"; // 点播服务接入区域 DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); return client; } catch (Exception e) { e.printStackTrace(); } return null; } }
标签:upload html googl imp stc beans ota hup ons
原文地址:https://www.cnblogs.com/pxblog/p/12238695.html