腾讯云 云点播 JAVASDK上传
温馨提示:
本文最后更新于 2024年06月10日
,已超过 313 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
maven
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>vod_api</artifactId>
<version>2.1.5</version>
//如果项目无slf4j-log4j12和cos_api不需要增加此代码
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
</exclusion>
</exclusions>
</dependency>
服务器上传
public static String uploadFile(MultipartFile multipartFile) {
try {
//生成文件名
String fileName = UUID.randomUUID().toString() + StrPool.DOT + FilenameUtils.getExtension(multipartFile.getOriginalFilename());
String tenant = BaseContextHandler.getTenant();
//日期文件夹
String relativePath = Paths.get(tenant, LocalDate.now().format(DateTimeFormatter.ofPattern(DEFAULT_MONTH_FORMAT_SLASH))).toString();
// web服务器存放的绝对路径
String absolutePath = Paths.get("/data/projects/uploadfile/file/", relativePath).toString();
java.io.File outFile = new java.io.File(Paths.get(absolutePath, fileName).toString());
org.apache.commons.io.FileUtils.writeByteArrayToFile(outFile, multipartFile.getBytes());
String url = new StringBuilder("")
.append(absolutePath)
.append(StrPool.SLASH)
.append(fileName)
.toString();
//替换掉windows环境的\路径
url = StrUtil.replace(url, "\\\\", StrPool.SLASH);
url = StrUtil.replace(url, "\\", StrPool.SLASH);
//url用来获取上传文件的本地位置
return url;
} catch (CosClientException e){
e.printStackTrace();
throw new BizException(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
throw new BizException(e.getMessage());
}
}
简单上传
https://cloud.tencent.com/document/product/266/10276
//url为本地文件URL地址
public static VodUploadResponse simpleUpload(String url) {
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
VodUploadClient client = new VodUploadClient("secretId", "secretKey");
VodUploadRequest request = new VodUploadRequest();
//任务流名称 如果不走任务流会播放器会出现 1005错误
request.setProcedure("LongVideoPreset");
//本地视频地址
request.setMediaFilePath("C:\\Users\\Administrator\\Videos\\金舟录屏大师\\jzrecord_2020-08-28-17-34-54.mp4");
try {
VodUploadResponse response = client.upload("ap-guangzhou", request);
System.out.println("Upload FileId = {"+response.getFileId()+"}");
return response;
} catch (Exception e) {
// 业务方进行异常处理
System.out.println("Upload Err = {"+e+"}");
throw new BizException(e.getMessage());
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no">
<title>腾讯云视频点播示例</title>
<!-- 引入播放器 css 文件 -->
<link href="//cloudcache.tencent-cloud.com/open/qcloud/video/tcplayer/tcplayer.css" rel="stylesheet">
<!-- 如需在IE8、9浏览器中初始化播放器,浏览器需支持Flash并在页面中引入 -->
<!--[if lt IE 9]>
<script src="//cloudcache.tencent-cloud.com/open/qcloud/video/tcplayer/ie8/videojs-ie8.js"></script>
<![endif]-->
<!-- 如果需要在 Chrome 和 Firefox 等现代浏览器中通过 H5 播放 HLS 格式的视频,需要在 tcplayer.v4.1.min.js 之前引入 hls.min.0.13.2m.js -->
<script src="//imgcache.qq.com/open/qcloud/video/tcplayer/libs/hls.min.0.13.2m.js"></script>
<!-- 引入播放器 js 文件 -->
<script src="//imgcache.qq.com/open/qcloud/video/tcplayer/tcplayer.v4.1.min.js"></script>
<!-- 示例 CSS 样式可自行删除 -->
</head>
<body>
<!-- 设置播放器容器 -->
<video id="player-container-id" preload="auto" width="1080" height="1920" playsinline webkit-playsinline x5-playsinline></video>
<!--
注意事项:
* 播放器容器必须为 video 标签
* player-container-id 为播放器容器的ID,可自行设置
* 播放器区域的尺寸请按需设置,建议通过 css 进行设置,通过css可实现容器自适应等效果
* playsinline webkit-playsinline x5-playsinline 这几个属性是为了在标准移动端浏览器不劫持视频播放的情况下实现行内播放,此处仅作示例,请按需使用
-->
<script>
var player = TCPlayer("player-container-id", { // player-container-id 为播放器容器ID,必须与html中一致
fileID: "fileID", // 请传入需要播放的视频fileID 必须
appID: "appID", // 请传入点播账号的子应用appID 必须
autoplay: false //是否自动播放
//其他参数请在开发文档中查看
});
</script>
</body>
</html>
正文到此结束
- 本文标签: 其他
- 本文链接: https://www.letcode.cn/article/82
- 版权声明: 本文由Jack.Chen原创发布,转载请遵循《 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权