大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
第一步导入
<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
然后登录腾讯云控制台的点播功能查看上传的视频 (需要转码成功后(会自动转码,需要一定时间))
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/222750.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...