官方文档地址:https://cloud.tencent.com/document/product/266/9219
官方Demo下载地址:http://video.qcloud.com/sdk/ugcuploader.html
一、jsp中引入上传按钮并引入js:
<tr id="upload_video" hidden>
<td style="width:20%">上传视频:<span style="color:red">*</span></td>
<td style="width:80%">
<input name="files" id="files" type="file" accept="video/*"/><br/>
<div id="id_test_video" style="width:100%; height:auto;"></div>
</tr>
//要放在第一个,不然js无法生效
<script src="//imgcache.qq.com/open/qcloud/js/vod/sdk/ugcUploader.js" type="text/javascript"></script>
二、新建获取签名时用到的实体类:
package com.hualife.bit.modules.prework.dao.ex.entity;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
public class Coursesiganture {
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;
}
}
三、controller中创建获取签名的方法:
@RequestMapping("/operate/signature.do")
@ResponseBody
public String signature() {
//SecretId 与 SecretKey 需要自己注册账号,然后申请
String SecretId = defKvMapper.selectByPrimaryKey("SecretId").getV();
String SecretKey = defKvMapper.selectByPrimaryKey("SecretKey").getV();
Coursesiganture sign = new Coursesiganture();
sign.setSecretId(SecretId);
sign.setSecretKey(SecretKey);
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) {
System.out.print("获取签名失败");
e.printStackTrace();
return "获取签名失败";
}
}
四、js中上传视频并返回回调结果的代码
//获取腾讯签名的方法
var getSignature = function(callback) {
$.ajax({
url : ctx + "/coursevideo/operate/signature.do",
type : 'POST',
dataType : 'json',
success : function(result) {
console.log(result);
callback(result);
}
});
};
//回调方法
qcVideo.ugcUploader.start({
videoFile : videoFile,
getSignature : getSignature,
allowAudio : 1,
success : function(result) {
alert("上传成功");
},
error : function(result) {
mini.alert("上传失败" + result.msg);
console.log(result)
},
progress : function(result) {
alert('正在上传,请稍后。。');
},
finish : function(result) {// 上传成功时的回调函数
console.log('上传结果的fileId:' + result.fileId);
console.log('上传结果的视频名称:' + result.videoName);
console.log('上传结果的视频地址:' + result.videoUrl);
}
});
五、回调函数弹出"上传成功"提示后,去自己申请的账户中管理视频地址,查看自己上传的视频就可以啦