阅读 93

Java实现腾讯云视频上传的功能

Java实现腾讯云视频上传的功能

1.上传视频的页面 


 


<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head>

    <meta charset="UTF-8">

    <title>Gary_Video</title>

</head>

<body>

 

<h1>Gary上传视频</h1>

 

<from id="from1">

    <input id="uploadVideoNow-file" type="file" onchange="changeInput(this)" style="display:none;">

 

</from>

 

<!-- a标签,当点击之后,执行change函数中的内容 -->

<a id="uploadVideoNow" href="javascript:void(0);" onclick="change()">点击上传视频</a>

 

<!-- 在web中引入sdk,js -->

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

<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>

<script type="text/javascript" th:inline="javascript">

 

 

    var getSignature = function(callback)

    {

        $.ajax({

            url:"/api/tengxun/sign",

                type:"POST",

        success:function(result)

    {

        alert(result);

        //即可上传视频

        callback(result);

    }

 

    })

    }

 

    function change()

    {

        $("#uploadVideoNow-file").click();

    }

 

    function changeInput(e){

        //alert(e.files[0].name);

        var videoFile = e.files[0];

        qcVideo.ugcUploader.start({

            //视频文件

            videoFile:videoFile,

            //上传位置

            getSignature:getSignature,

            //是否上传声音

            allowAudio:1,

            //上传成功

            success:function(result)

            {

                alert("success");

            },

            //上传失败

            error:function(result)

            {

                alert("error");

            },

            //上传过程中

            progress:function(result)

            {

 

            },

            //上传完成

            finish:function(result)

            {

                alert("finish");

            }

        })

    }

 

</script>

 

</body>

</html>

 

video.html

2.构建类


package com.hrhq.core.api.controller;

 

import sun.misc.BASE64Encoder;

 

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import java.net.URLEncoder;

 

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 + "&currentTimeStamp=" + 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;

 }

 

}

3.方法 腾讯云API


Signature.java得到加密后的字符串


  通过IndexController.java中的getSign()方法将appkey和appid进行加密,得到加密后的字符串并通过函数指针回传给getSignature:getSignature


package com.hrhq.core.api.controller;

 

 

 

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.servlet.ModelAndView;

 

import java.util.Random;

 

@RestController

@RequestMapping("/api/tengxun")

public class IndexController {

 

    @RequestMapping("/")

    public ModelAndView index(){

        return new ModelAndView("/video.html");

    }

 

    @RequestMapping("/sign")

    @ResponseBody

    public String getSign()

    {

        //得到Sign

        Signature sign = new Signature();

        //个人API密钥中的Secret Id

        sign.setSecretId("AKIDZw4wZwpFBsNcb5rM8fxzlHdjZ9TZ25Wn");

        //个人API密钥中的Secret Key

        sign.setSecretKey("A4j8HAoCPN6brsc7MNDn8D2f3B8qJYXK");

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

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

        sign.setSignValidDuration(3600 * 24 * 2);

 

        String signature = null;

        try {

            signature = sign.getUploadSignature();

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

        } catch (Exception e) {

            System.out.print("获取签名失败");

            e.printStackTrace();

        }

 

        return signature;

    }

 

}

 

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

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

原文链接:https://blog.csdn.net/lliuhongyang/article/details/97263389


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