阅读 28 SEO

web利用腾讯云点播上传视频

web利用腾讯云点播上传视频

web利用腾讯云点播上传视频到云服务器


第一步导入


<script src="//imgcache.qq.com/open/qcloud/js/vod/sdk/ugcUploader.js"></script>

第二步在服务端设置秘钥,我用的是javaee 编写一个Signature类 


所需jar包http://download.csdn.net/download/sinat_36596988/10195891


import javax.crypto.Mac;

 

import javax.crypto.spec.SecretKeySpec;

import Decoder.BASE64Encoder;

 

 

public class Signature {

private String secretId;

private String secretKey;

private long currentTime;

private int random;

private int signValidDuration;

 

 

private static final String HMAC_ALGORITHM = "HmacSHA1";

private static final String CONTENT_CHARSET = "UTF-8";

 

 

public static byte[] byteMerger(byte[] byte1, byte[] byte2) {

byte[] byte3 = new byte[byte1.length + byte2.length];

System.arraycopy(byte1, 0, byte3, 0, byte1.length);

System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);

return byte3;

}

public String getUploadSignature() throws Exception {

String strSign = "";

String contextStr = "";

 

 

long endTime = (currentTime + signValidDuration);

contextStr += "secretId=" + java.net.URLEncoder.encode(secretId, "utf8");

contextStr += "¤tTimeStamp=" + currentTime;

contextStr += "&expireTime=" + endTime;

contextStr += "&random=" + random;

try {

Mac mac = Mac.getInstance(HMAC_ALGORITHM);

SecretKeySpec secretKey = new SecretKeySpec(this.secretKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm());

mac.init(secretKey);

byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET));

byte[] sigBuf = byteMerger(hash, contextStr.getBytes("utf8"));

strSign = new String(new BASE64Encoder().encode(sigBuf).getBytes());

strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");

} catch (Exception e) {

throw e;

}

return strSign;

}

 

 

public void setSecretId(String secretId) {

this.secretId = secretId;

}

 

 

public void setSecretKey(String secretKey) {

this.secretKey = secretKey;

}

 

 

public void setCurrentTime(long currentTime) {

this.currentTime = currentTime;

}

 

 

public void setRandom(int random) {

this.random = random;

}

 

 

public void setSignValidDuration(int signValidDuration) {

this.signValidDuration = signValidDuration;

}

}

//通过shangchuan.do方法得到秘钥

//APPID APPKEY 进入腾讯云控制台获取https://console.cloud.tencent.com/cam/capi

@RequestMapping("shangchuan.do")

@ResponseBody

public String shangchuan(Model model){

Signature sign = new Signature();

sign.setSecretId("你的APPID");

sign.setSecretKey("你的APPKEY");

sign.setCurrentTime(System.currentTimeMillis() / 1000);

sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));

sign.setSignValidDuration(3600 * 24 * 2);

 

 

try {

String signature = sign.getUploadSignature();

//System.out.println("signature : " + signature);

return signature;

} catch (Exception e) {

e.printStackTrace();

return "获取签名失败";

}

}

 


 


 

 


 


第三步通过ajax方法得到秘钥并且上传

<html>

<head>

<title>My JSP 'shang.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<script src="//imgcache.qq.com/open/qcloud/js/vod/sdk/ugcUploader.js"></script>

<script src="js/jquery.min.js"></script>

</head>

<body>

<form id="form1">

<input id="uploadVideoNow-file" type="file" />

</form>

</body>

</html>

<script>

var getSignature = function(callback) {

$.ajax({

url : "shangchuan.do", //服务器获取客户端上传签名的URL

type : "POST",

success : function(result) {

//result.returnData.signature为获取到的签名

callback(result);

//callback(result.returnData.signature);

}

});

};

 

 

$('#uploadVideoNow-file').on('change', function(e) {

var videoFile = this.files[0];

var resultMsg = qcVideo.ugcUploader.start({

videoFile : videoFile,

getSignature : getSignature,

allowAudio : 1,

isTranscode: 1,

success : function(result) {

alert("上传成功");

},

error : function(result) {

alert("上传失败");

},

progress : function(result) {

alert("上传进度:" + result.curr);

},

finish : function(result) {

alert("上传完成");

}

});

});

</script>

具体事件https://cloud.tencent.com/document/product/266/9239 


然后登录腾讯云控制台的点播功能查看上传的视频 (需要转码成功后(会自动转码,需要一定时间))

————————————————

版权声明:本文为CSDN博主「gentle+」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/sinat_36596988/article/details/79010636


文章分类
后端
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐