zyh
7 小时以前 466cc943089116f996db0c2f249878a74fb1cc10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.zy.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
 
@Component
public class HikUtils {
 
    @Value("${zyHikUrl}")
    private String hikUrl;
 
    public void startPic(String ip, String filename) {
        try {
            if (hikUrl == null || "".equals(hikUrl)) {
                News.error("hikUrl配置为空,无法进行拍摄");
                return;
            }
            if (ip == null || "".equals(ip)) {
                News.error("IP地址为空,无法进行拍摄");
                return;
            }
            if (filename == null || "".equals(filename)) {
                News.error("文件名为空,无法进行拍摄");
                return;
            }
            HashMap<String, Object> data = new HashMap<>();
            data.put("ip", ip);
            data.put("filename", filename);
 
            String response = new HttpHandler.Builder()
                    .setUri(hikUrl)
                    .setPath("/capture")
                    .setJson(JSON.toJSONString(data))
                    .build()
                    .doPost();
            if (response == null || "".equals(response)) {
                News.error("请求接口失败,返回值为空");
                return;
            }
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject == null) {
                News.error("解析响应失败,返回值不是有效的JSON");
                return;
            }
            Boolean success = jsonObject.getBoolean("success");
            if (success != null && success) {
                News.error("请求接口成功!!!url:{};request:{};response:{}", hikUrl + "/capture", JSON.toJSONString(data), response);
            } else {
                News.error("请求接口失败!!!url:{};request:{};response:{}", hikUrl + "/capture", JSON.toJSONString(data), response);
            }
        } catch (Exception e) {
            News.error("拍摄失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }
 
}