javaweb腾讯云点播上传视频(附加所需jar包)

用web上传视频到你的腾讯云服务器。

 

首先需要购买腾讯云点播,获得你的云api密匙来操作你名下的所有腾讯云资源。

javaweb腾讯云点播上传视频(附加所需jar包)_第1张图片

第一步:加入所需jar包  地址:https://download.csdn.net/download/weixin_42726550/10631138

第二步:构建你的Signature类

import sun.misc.BASE64Encoder;

import java.net.URLEncoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

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 = this.currentTime + this.signValidDuration;
        contextStr = contextStr + "secretId=" + URLEncoder.encode(this.secretId, "utf8");
        contextStr = contextStr + "¤tTimeStamp=" + this.currentTime;
        contextStr = contextStr + "&expireTime=" + endTime;
        contextStr = contextStr + "&random=" + this.random;
        contextStr = contextStr + "&procedure=QCVB_SimpleProcessFile(1,1)";
        try
        {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secretKey = new SecretKeySpec(this.secretKey.getBytes("UTF-8"), mac.getAlgorithm());
            mac.init(secretKey);
            byte[] hash = mac.doFinal(contextStr.getBytes("UTF-8"));
            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;
    }
}

 UserController代码

 

 //通过shangchuan.do方法得到秘钥
//APPID APPKEY 进入腾讯云控制台获取https://console.cloud.tencent.com/cam/capi
    @RequestMapping(value = "/shangchuan.do")
    @ResponseBody
    public String shangchuan(Model model){
        Signature sign = new Signature();
        sign.setSecretId("你的id");
        sign.setSecretKey("你的key");

        sign.setCurrentTime(System.currentTimeMillis() / 1000L);
        sign.setRandom(new Random().nextInt(2147483647));
        sign.setSignValidDuration(172800);

        try
        {
            return sign.getUploadSignature();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "获取签名失败";

    }

 

 前台html代码











22



 

 成功后会alert输出进度,完成时为1,alert上传成功和完成

上传失败信息可以在error里加 alert(result.msg);来查看错误信息

请求签名校验失败一般是jar包不对,我上传的jar包是对的。

 

你可能感兴趣的:(java)