保利威视根据vid查询视频信息

最近对接的视频存储平台是保利威视,业务需要视频的详细信息,整理的以下代码

保利威视API 的地址 http://dev.polyv.net/2017/videoproduct/v-api/v-api-vmanage/v-api-vmanage-info/get-video-msg/

import com.alibaba.fastjson.JSONObject;
import com.colorzebra.message.model.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class TestServiceImpl {

    public Integer test(String vid) throws DigestException {
        //需要视频的长度 单位是s
        int second=0;
        //指定返回值的类型
        String  format = "json";
        //生成毫秒级的时间戳
        long ptime = new Date().getTime();
        //将时间戳转为String类型
        String time = String.valueOf(ptime);
        //拼接字符串
        /*
        签名规则:将非空的请求参数按照参数名字典顺序排列,连接参数名与参数值,并在尾部加上secretkey,生成40位大写SHA1值,作              为sign。 以下是示例过程 */
        //连接参数名与参数值,并在尾部加上secretkey(从点播后台获取,假设secretkey的值为tIQp4ATe9Z),如下:
        format="format=json&ptime="+ptime+"&vid="+vid+"tIQp4ATe9Z";
        String sha1 = DigestUtils.sha1Hex(format);
        //获取SHA1 加密
        String s = SHA1(format);
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost("http://api.polyv.net/v2/video/6d93d0897a/get-video-msg");
        // 创建参数队列
        List formparams = new ArrayList();
        formparams.add(new BasicNameValuePair("vid", vid));
        formparams.add(new BasicNameValuePair("format", "json"));
        formparams.add(new BasicNameValuePair("ptime", time));
        formparams.add(new BasicNameValuePair("sign", s));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    //处理返回值
                    String a= EntityUtils.toString(entity, "UTF-8");
                    JSONObject  jsonObject = JSONObject.parseObject(a);
                    Map<String,Object> map = (Map<String,Object>)jsonObject;
                    List sa = (List) map.get("data");
                    Map<String,Object> data= (Map<String, Object>) sa.get(0);
                    //获取视频时长
                    String time1= (String) data.get("duration");
                    //处理视频时长 转化为秒
                    String[]  strs=time1.split(":");
                    try {
                        int h = Integer.parseInt(strs[0]);
                        int m = Integer.parseInt(strs[1]);
                        second = Integer.parseInt(strs[2]);
                        if(h>0){
                            second+= h*60*60;
                        }if( m>0){
                            second+=m*60;
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {

        }   finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return second;
    }

    public static String SHA1(String maps) throws DigestException {
        //获取信息摘要 - 参数字典排序后字符串
        try {
            //指定sha1算法
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(maps.getBytes());
            //获取字节数组
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString().toUpperCase();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new DigestException("签名错误!");
        }
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值