一、Java上传SDK
文档地址:https://help.aliyun.com/document_detail/53406.html?spm=a2c4g.11186623.6.1065.4068192b1bTgc5
SDK下载地址页面:https://help.aliyun.com/document_detail/51992.html?spm=a2c4g.11186623.2.25.34ea3815KgAGSf
1、安装SDK
jar包未开源,不能直接在maven中央仓库下载到,手动吧依赖安装到本地仓库中,使用maven命令进行安装。
解压下载好的压缩包,在lib目录下进入通过CMD进入命令行模式,输入命令:
mvn install:install-file
-DgroupId=com.aliyun
-DartifactId=aliyun-sdk-vod-upload
-Dversion=1.4.12
-Dpackaging=jar
-Dfile=aliyun-java-vod-upload-1.4.12.jar
现在jar包安装好了。
2、配置properties文件
#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=<您的AccessKeyId>
aliyun.vod.file.keysecret=<您的AccessKeySecret>
#为了可以上传正常大小文件 配置以下
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
3、编写从配置文件中取值工具
@Component
public class ConstantPropertiesUtil implements InitializingBean {
@Value("${aliyun.vod.file.keyid}")
private String keyId;
@Value("${aliyun.vod.file.keysecret}")
private String keySecret;
public static String KEY_ID;
public static String KEY_SECRET;
@Override
public void afterPropertiesSet() throws Exception {
KEY_ID = keyId;
KEY_SECRET = keySecret;
}
}
4、编写视频上传代码(流式上传)
完整示例代码在阿里云sdk的jar包中
https://help.aliyun.com/document_detail/53406.html?spm=a2c4g.11186623.4.2.394c7bdcZARxCA
@Service
public class VodServiceImpl implements VodService {
@Override
public String uploadVideoAly(MultipartFile file) {
try{
String accessKeyId = ConstantPropertiesUtil.KEY_ID;
String accessKeySecret = ConstantPropertiesUtil.KEY_SECRET;
//fileName: 上传文件的原始名
String fileName = file.getOriginalFilename();
//title: 上传后显示的名称
/**
* substring的作用:
* 假如你要上传的文件名为小猪佩奇.mp4,
* substring(0,fileName.lastIndexOf("."))
* 可以帮助你把文件名截取为 小猪佩奇
*
* 该函数为:substring(int beginIndex,int endIndex)
* fileName.lastIndexOf(".")即为文件名中的最后一个.的下标
*/
String title = fileName.substring(0,fileName.lastIndexOf("."));
//上传文件的输入流
InputStream inputStream = file.getInputStream();
//创建上传请求
UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
//创建上传实例
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
//定义一个videoId
String videoId = null;
if (response.isSuccess()){
videoId = response.getVideoId();
}else {
videoId = response.getVideoId();
}
return videoId;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
二、根据阿里云点播控制台的视频ID获取视频播放地址及视屏播放凭证
文档地址:https://help.aliyun.com/document_detail/57756.html?spm=a2c4g.11186623.6.906.11a83bd995vyLf
1、初始化对象
public class InitObject {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
2、根据视频ID获取视频播放地址
public static void getPlayUrl()throws Exception{
//根据视频id获取视频播放地址
//创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
//创建获取视频地址request和response
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
//向request对象里面设置视频id
request.setVideoId("<您的视频ID>","VideoId1","VideoId2"....);
//调用初始化对象里面的方法,传递request,获取数据
response = client.getAcsResponse(request);
//如果有多个视频的话
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");
}
3、根据视频ID获取视频播放凭证
public static void getPlayAuth() throws Exception{
//根据视频id获取视频播放凭证
//创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
//创建获取视频凭证request和response
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//向request对象里面设置视频id
request.setVideoId("<您的视频ID>");
//调用初始化对象里面的方法,得到凭证
response = client.getAcsResponse(request);
System.out.println("playauth:"+response.getPlayAuth());
}
三、报错
1、
java.lang.ClassNotFoundException: com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest
说明缺少依赖:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
</dependency>
2、
java.lang.ClassNotFoundException: com.aliyun.oss.common.utils.StringUtils
说明缺少依赖:
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
3、
java.lang.ClassNotFoundException: com.alibaba.fastjson.JSON
说明缺少依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
总共需要的依赖:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>