diff --git a/libs/artemis-http-client-1.1.11.RELEASE.jar b/libs/artemis-http-client-1.1.11.RELEASE.jar new file mode 100644 index 0000000..880448e Binary files /dev/null and b/libs/artemis-http-client-1.1.11.RELEASE.jar differ diff --git a/pom.xml b/pom.xml index 3b3a5b4..6ee6380 100644 --- a/pom.xml +++ b/pom.xml @@ -106,6 +106,14 @@ log4j 1.2.14 + + + hk + artemis-http-client + 1.1.11.RELEASE + system + ${pom.basedir}/libs/artemis-http-client-1.1.11.RELEASE.jar + diff --git a/src/main/java/com/cm/population/utils/Tools.java b/src/main/java/com/cm/population/utils/Tools.java new file mode 100644 index 0000000..ff42c43 --- /dev/null +++ b/src/main/java/com/cm/population/utils/Tools.java @@ -0,0 +1,58 @@ +package com.cm.population.utils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * 工具类 + * + * @author + * @create 2019-01-11 17:06 + **/ +public class Tools { + + /** + * 将图片写到 硬盘指定目录下 + * + * @param in + * @param dirPath + * @param filePath + */ + public static void savePicToDisk(InputStream in, String dirPath, + String filePath) { + + try { + File dir = new File(dirPath); + if (dir == null || !dir.exists()) { + dir.mkdirs(); + } + + //文件真实路径 + String realPath = dirPath.concat(filePath); + File file = new File(realPath); + if (file == null || !file.exists()) { + file.createNewFile(); + } + + FileOutputStream fos = new FileOutputStream(file); + byte[] buf = new byte[1024]; + int len = 0; + while ((len = in.read(buf)) != -1) { + fos.write(buf, 0, len); + } + fos.flush(); + fos.close(); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/test/java/HKTest.java b/src/test/java/HKTest.java new file mode 100644 index 0000000..8f1febc --- /dev/null +++ b/src/test/java/HKTest.java @@ -0,0 +1,210 @@ +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.cm.population.utils.Tools; +import com.hikvision.artemis.sdk.ArtemisHttpUtil; +import com.hikvision.artemis.sdk.config.ArtemisConfig; +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import static com.hikvision.artemis.sdk.util.HttpUtil.wrapClient; + +public class HKTest { + + /** + * 请根据技术支持提供的实际的平台IP/端口和API网关中的合作方信息更换static静态块中的三个参数. + * [1 host] + * host格式为IP:Port,如10.0.0.1:443 + * 当使用https协议调用接口时,IP是平台(nginx)IP,Port是https协议的端口; + * 当使用http协议调用接口时,IP是artemis服务的IP,Port是artemis服务的端口(默认9016)。 + * [2 appKey和appSecret] + * 请按照技术支持提供的合作方Key和合作方Secret修改 + * appKey:合作方Key + * appSecret:合作方Secret + * 调用前看清接口传入的是什么,是传入json就用doPostStringArtemis方法,是表单提交就用doPostFromArtemis方法 + * + */ + /** + * API网关的后端服务上下文为:/artemis + */ + private static final String ARTEMIS_PATH = "/artemis"; + + /** + * 调用POST请求类型接口,这里以获取组织列表为例 + * 接口实际url:https://ip:port/artemis/api/resource/v1/org/orgList + * @return + */ + public static String callPostApiGetOrgList() throws Exception { + /** + * https://ip:port/artemis/api/resource/v1/org/orgList + * 通过查阅AI Cloud开放平台文档或网关门户的文档可以看到获取组织列表的接口定义,该接口为POST请求的Rest接口, 入参为JSON字符串,接口协议为https。 + * ArtemisHttpUtil工具类提供了doPostStringArtemis调用POST请求的方法,入参可传JSON字符串, 请阅读开发指南了解方法入参,没有的参数可传null + */ + ArtemisConfig config = new ArtemisConfig(); + config.setHost("1.30.233.210:56443"); // 代理API网关nginx服务器ip端口 + config.setAppKey("27404117"); // 秘钥appkey + config.setAppSecret("VxXrlPboTJI7ygpf4WBL");// 秘钥appSecret +// final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList"; + + // {"code":"0","msg":"SUCCESS","data":{"total":1,"list":[{"name":"管委会中心端","indexCode":"root00000000","treeCode":"0","parentIndexCode":"-1","externalIndexCode":"1502"}]}} + final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/regions/root"; + Map paramMap = new HashMap();// post请求Form表单参数 + paramMap.put("pageNo", "1"); + paramMap.put("pageSize", "2"); + String body = JSON.toJSON(paramMap).toString(); + Map path = new HashMap(2) { + { + put("https://", getCamsApi); + } + }; + return ArtemisHttpUtil.doPostStringArtemis(config,path, body, null, null, "application/json"); + } + + /** + * 摄像头资源 + * @return + * @throws Exception + */ + public static String callPostApiGetCameras() throws Exception { + ArtemisConfig config = new ArtemisConfig(); + config.setHost("1.30.233.210:56443"); // 代理API网关nginx服务器ip端口 + config.setAppKey("27404117"); // 秘钥appkey + config.setAppSecret("VxXrlPboTJI7ygpf4WBL");// 秘钥appSecret + final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/cameras"; + Map paramMap = new HashMap();// post请求Form表单参数 + paramMap.put("pageNo", "1"); + paramMap.put("pageSize", "2"); +// paramMap.put("regionIndexCodes", JSON.toJSONString(Arrays.asList("root00000000"))); + String body = JSON.toJSON(paramMap).toString(); + Map path = new HashMap(2) { + { + put("https://", getCamsApi); + } + }; + return ArtemisHttpUtil.doPostStringArtemis(config,path, body, null, null, "application/json"); + } + + public static String callPostApiGetPreview() throws Exception { + ArtemisConfig config = new ArtemisConfig(); + config.setHost("1.30.233.210:56443"); // 代理API网关nginx服务器ip端口 + config.setAppKey("27404117"); // 秘钥appkey + config.setAppSecret("VxXrlPboTJI7ygpf4WBL");// 秘钥appSecret + final String getCamsApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs"; + Map paramMap = new HashMap();// post请求Form表单参数 + paramMap.put("cameraIndexCode", "8d03580e6ffc40678000b6b1f4fe1918"); + String body = JSON.toJSON(paramMap).toString(); + Map path = new HashMap(2) { + { + put("https://", getCamsApi); + } + }; + return ArtemisHttpUtil.doPostStringArtemis(config,path, body, null, null, "application/json"); + } + + + + /** + * 调用POST请求类型接口,这里以分页获取区域列表为例 + * 接口实际url:https://ip:port/artemis/api/api/resource/v1/regions + * @return + */ + public static String callPostApiGetRegions() throws Exception { + /** + * https://ip:port/artemis/api/resource/v1/regions + * 过查阅AI Cloud开放平台文档或网关门户的文档可以看到分页获取区域列表的定义,这是一个POST请求的Rest接口, 入参为JSON字符串,接口协议为https。 + * ArtemisHttpUtil工具类提供了doPostStringArtemis调用POST请求的方法,入参可传JSON字符串, 请阅读开发指南了解方法入参,没有的参数可传null + */ + ArtemisConfig config = new ArtemisConfig(); + config.setHost("127.0.0.1"); // 代理API网关nginx服务器ip端口 + config.setAppKey("20469790"); // 秘钥appkey + config.setAppSecret("lofnD6DbnBllHmk5YOyx");// 秘钥appSecret + final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/regions"; + Map paramMap = new HashMap();// post请求Form表单参数 + paramMap.put("pageNo", "1"); + paramMap.put("pageSize", "2"); + paramMap.put("treeCode", "0"); + String body = JSON.toJSON(paramMap).toString(); + Map path = new HashMap(2) { + { + put("https://", getCamsApi); + } + }; + return ArtemisHttpUtil.doPostStringArtemis(config,path, body, null, null, "application/json"); + } + + /** + * 调用POST接口,返回图片 + * 接口实际url:https://ip:port/artemis/api/visitor/v1/record/pictures + * @return + */ + public static String callPostImgs() throws Exception { + ArtemisConfig config = new ArtemisConfig(); + config.setHost("127.0.0.1"); // 代理API网关nginx服务器ip端口 + config.setAppKey("20469790"); // 秘钥appkey + config.setAppSecret("lofnD6DbnBllHmk5YOyx");// 秘钥appSecret + final String getSecurityApi = "/artemis" + "/api/visitor/v1/record/pictures"; // 接口路径 + Map path = new HashMap(2) { + { + put("https://", getSecurityApi); + } + }; + Map head = new HashMap(2) { //get请求的head参数 + { + put("headpost", "sky-test"); + } + }; + Map query = new HashMap(2) { //get请求的head参数 + { + put("domainId", "0"); + } + }; + JSONObject jsonBody = new JSONObject(); + jsonBody.put("svrIndexCode", "9ff58bc2-65a5-464b-b28c-daea67ba9569"); + jsonBody.put("picUri", "/pic?9dda12i40-e*5b84626c4105m5ep=t=i3p*i=d1s*i=d3b*i1d3b*855925cea-96008b--2718943z855s=5i76="); + String body = jsonBody.toJSONString(); + //参数根据接口实际情况设置 + HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(config, path, body, query, null,"application/json",head); + try { + HttpResponse resp = result; + if (302==resp.getStatusLine().getStatusCode()) { + /* + 获取图片数据保存到本地 + 注:1.对于有时效的图片,必须尽快保存到本地 + 2.若无时效,则可以直接保存location,后续自行访问获取 + */ + Header header= resp.getFirstHeader("location"); + String newUrl = header.getValue(); + HttpGet httpget = new HttpGet(newUrl); + HttpClient httpClient = wrapClient(httpget.getURI().getScheme()+"://"+httpget.getURI().getHost()); + HttpResponse execute = httpClient.execute(httpget); + HttpEntity entity = execute.getEntity(); + InputStream in = entity.getContent(); + Tools.savePicToDisk(in, "d:/", "test311.jpg"); + }else{ + System.out.println("下载出错"); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return getSecurityApi; + } + + public static void main(String[] args) throws Exception { +// String result = callPostApiGetOrgList(); +// String result = callPostApiGetCameras(); + String result = callPostApiGetPreview(); + JSONObject jsonObject = JSONObject.parseObject(result); + System.out.println(jsonObject); +// String VechicleDataResult = callPostApiGetRegions(); +// System.out.println(VechicleDataResult); + } + +}