專案整合騰訊雲點播平臺
一、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);
}
});
五、回撥函式彈出"上傳成功"提示後,去自己申請的賬戶中管理視訊地址,檢視自己上傳的視訊就可以啦