java对接海康视频

前言

由于前端存在跨域,所以后端实现转发以及将加密放在后端处理,将获取的xml返回前端

一、海康视频demo

链接:https://pan.baidu.com/s/1lFFfP_H2ckBhbIQk6TdueQ
提取码:qxc6

二、实现

2.1 控制层

@PostMapping("getPreviewParamByCameraUuid")
    public ResultVO getPreviewParamByCameraUuid(@RequestBody JSONObject requestJSON) {
      String url= "http://61.240.12.213:84/openapi/service/vss/preview/getPreviewParamByCameraUuid?token=";
        String xml="";
        hikVisionConfig.setCameraUuid(requestJSON.getString("cameraUuid"));
        hikVisionConfig.setTime(System.currentTimeMillis());
        String postDataJSON = HikVisionUtil.createPostDataJSON(hikVisionConfig);
        String token = HikVisionUtil.createToken(hikVisionConfig);
        url = url.replace("", token);
        try {
            xml= YHttpRequestUtil.postJson(url,postDataJSON);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(token!=null || "".equals(token)){
            ResultVOUtil.error(400,"token为空");
        }
        return ResultVOUtil.success(xml);
    }

2.2 配置实体及配置文件

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenxd
 * @create 2019-11-11 18:28
 */
@Configuration
@ConfigurationProperties(prefix="hikvision")
public class HikVisionConfig {

    private String appkey;

    private String opUserUuid;

    private String cameraUuid;

    private String netZoneUuid;

    private Long time;
	//省略setter,getter
}

配置文件代码

#海康视频
hikvision.appkey = 28b883c2
hikvision.opUserUuid = 5b2eb534696b11e89c2e438f92627767
hikvision.netZoneUuid = edd45ccd54a3465e8939eab9c28d65e1

2.3 工具类

package com.ms.base.utils;

import com.ms.base.config.HikVisionConfig;

/**
 * 海康视频工具类
 * @author chenxd
 * @create 2019-11-11 15:17
 */
public class HikVisionUtil {

    private static String mySecret = "d2376a5e13f840cdbd781a3c1bc8e593";

    private static String uri = "/openapi/service/vss/preview/getPreviewParamByCameraUuid";

    /**
     * 创建post请求的json字符串
     * @param hikVisionConfig
     * @return
     */
    public static String createPostDataJSON(HikVisionConfig hikVisionConfig){
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        sb.append("\"appkey\":\""+hikVisionConfig.getAppkey()+"\",");
        sb.append("\"time\":\""+hikVisionConfig.getTime()+"\",");
        sb.append("\"opUserUuid\":\""+hikVisionConfig.getOpUserUuid()+"\",");
        sb.append("\"cameraUuid\":\""+hikVisionConfig.getCameraUuid()+"\",");
        sb.append("\"netZoneUuid\":\""+hikVisionConfig.getNetZoneUuid()+"\"");
        sb.append("}");
        return sb.toString();
    }

	//创建token
    public static String createToken(HikVisionConfig hikVisionConfig){

        String srcStr = uri+createPostDataJSON(hikVisionConfig)+mySecret;
        return Md5Utils.hash(srcStr).toUpperCase();
    }
}
	/**
     * post请求,参数为json
     * @param url
     * @param jsonStr
     * @return
     * @throws IOException
     */
    public static String postJson(String url, String jsonStr) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        StringEntity stringEntity = new StringEntity(jsonStr, "application/json", "utf-8");
        httpPost.setEntity(stringEntity);

        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
            HttpEntity resultEntity = response.getEntity();
            InputStream inputStream = resultEntity.getContent();
            String content = IOUtils.toString(inputStream);
            EntityUtils.consume(resultEntity);//目的是关闭流
            return content;
        } finally {
            httpPost.releaseConnection();
            if (response != null)
                response.close();
        }
    }
package com.ms.base.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;

/**
 * Md5加密方法
 * 
 */
public class Md5Utils
{
    private static final Logger log = LoggerFactory.getLogger(Md5Utils.class);

    private static byte[] md5(String s)
    {
        MessageDigest algorithm;
        try
        {
            algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(s.getBytes("UTF-8"));
            byte[] messageDigest = algorithm.digest();
            return messageDigest;
        }
        catch (Exception e)
        {
            log.error("MD5 Error...", e);
        }
        return null;
    }

    private static final String toHex(byte hash[])
    {
        if (hash == null)
        {
            return null;
        }
        StringBuffer buf = new StringBuffer(hash.length * 2);
        int i;

        for (i = 0; i < hash.length; i++)
        {
            if ((hash[i] & 0xff) < 0x10)
            {
                buf.append("0");
            }
            buf.append(Long.toString(hash[i] & 0xff, 16));
        }
        return buf.toString();
    }

    public static String hash(String s)
    {
        try
        {
            return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8");
        }
        catch (Exception e)
        {
            log.error("not supported charset...{}", e);
            return s;
        }
    }
}

三、结果

请求地址:http://127.0.0.1:9030/v7/warning/getPreviewParamByCameraUuid
请求参数

{
	"cameraUuid":"331719906dae456f8ffdbf65d2a93066"
}

返回示例

{
    "code": 200,
    "msg": "成功",
    "results": "{\"errorCode\":0,\"errorMessage\":\"fetchPreviewXml success!\",\"data\":\"\\n\"}"
}

你可能感兴趣的:(java)