HttpUtil.java
14.7 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.sincere.haikangface.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sincere.haikangface.bean.face.PermissionBean;
import com.sincere.haikangface.bean.face.PermissionDHBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* http 工具类
*/
@Slf4j
public class HttpUtil {
public static String post(String requestUrl, String accessToken, String params)
throws Exception {
String contentType = "application/json";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
public static String post(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
/**
* 下发人脸值253服务设备
* @param filePath
* @param card
* @param name
* @param deviceId
* @param startTime
* @param endTime
* @param validTimeEnabled
* @param userType
*/
public static void uploadImgs(String filePath, String card, String name, String deviceId, String startTime,
String endTime, int validTimeEnabled, String userType,Integer schoolId) {
if (!new File(filePath).exists()) {
log.error("图片不存在,图片路径:{}",filePath);
return;
}
try{
String url = "http://120.26.116.253:8089/file/uploadImg";
// String url = "http://127.0.0.1:8089/file/uploadImg";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
headers.setContentType(mediaType);
MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
multivaluedMap.add("file", fileSystemResource);
multivaluedMap.add("deviceId", deviceId);
multivaluedMap.add("card", card);
multivaluedMap.add("name", name);
multivaluedMap.add("startTime", startTime);
multivaluedMap.add("endTime", endTime);
multivaluedMap.add("validTimeEnabled", validTimeEnabled);
multivaluedMap.add("userType", userType);
multivaluedMap.add("schoolId", schoolId);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multivaluedMap, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
log.info("发送请求,下发人脸至253服务器海康设备,请求地址:{} ,下发用户名:{},返回信息:{}",url,name,responseEntity.getBody());
}catch (Exception e){
log.error("发送请求,下发人脸至253服务器海康设备,发生异常: {}",e);
}
}
public static boolean IsDeviceOnline(String deviceId) {
String url = "http://120.26.116.253:8089/file/IsDeviceOnline?deviceId=" + deviceId;
RestTemplate restTemplate = new RestTemplate();
String res = restTemplate.getForObject(url, String.class);
return res.equals("1");
}
/**
* 删除海康设备卡信息
* @param deviceId
* @param card
* @return
*/
public static boolean deleteCard(String deviceId, String card) {
String url = "http://120.26.116.253:8089/file/DeleteCard?deviceId=" + deviceId + "&card=" + card;
RestTemplate restTemplate = new RestTemplate();
String res = restTemplate.getForObject(url, String.class);
return res.equals("1");
}
/**
* 获取海康设备卡信息
* @param deviceId
* @param card
* @return
*/
public static boolean getCard(String deviceId, String card) {
String url = "http://120.26.116.253:8089/file/getCard?deviceId=" + deviceId + "&card=" + card;
RestTemplate restTemplate = new RestTemplate();
String res = restTemplate.getForObject(url,String.class);
return res.equals("1");
}
/**
* 下发权限至海康设备
* @param permissionBean
*/
public static void sendPermission2HK(PermissionBean permissionBean) {
try {
String url = "http://120.26.116.253:8089/file/sendPermiss";
if (null != permissionBean) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
headers.setContentType(mediaType);
HttpEntity<PermissionBean> httpEntity = new HttpEntity<>(permissionBean, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
String reqJson= JSON.toJSONString(permissionBean);
log.info("发送请求,下发权限至海康设备,请求地址:{},请求参数:{},返回信息:{}",url,reqJson,responseEntity.getBody());
}
} catch (Exception e) {
log.error("发送请求,下发权限至海康设备,发生异常: {}",e);
}
}
/**
* 百度人脸识别查询
* @param file
* @param schoolId
* @return
*/
public static JSONObject sendToKB(File file,String schoolId) {
String url = "http://120.26.116.253:9214/baiduapi/search/" + schoolId;
MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();
HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();
try {
MediaType mediaType = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
headers.setContentType(mediaType);
FileSystemResource fileSystemResource = new FileSystemResource(file);
multivaluedMap.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multivaluedMap, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
log.info("发送请求,人脸识别查询,请求地址:{},返回信息:{}",url,jsonObject.toJSONString());
return jsonObject;
} catch (Exception e) {
log.info("发送请求,百度人脸识别失败,异常信息:{}",e);
}
return null;
}
/**
* 下发单个人脸至大华设备
* @param filePath
* @param schoolId
* @param studentCode
* @param clintType
* @param deviceId
* @return
*/
public static void uploadDHImgForOne(String filePath,Integer schoolId,String studentCode,int clintType,String deviceId) {
String api = "http://114.55.30.100:8991/user/uploadImgAndUserInfo";
try {
String url = String.format(api +"?schoolId=%s&studentCode=%s&deviceId=%s&clint_type=%s&file=%s",schoolId,studentCode,deviceId,clintType,filePath);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
log.info("发送请求,下发单个人脸至大华设备,请求地址:{} ,返回信息:{}",url, responseEntity.getBody());
} catch (Exception e) {
log.error("发送请求,下发单个人脸至大华,异常信息: {}",e);
}
}
/**
* 下发权限至大华设备
* @param permissionBean
* @return
*/
public static void sendDHPermission(PermissionDHBean permissionBean) {
try{
String url = "http://121.40.109.21:8991/file/sendPermission";
// String url = "http://127.0.0.1:8991/file/sendPermission";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
headers.setContentType(mediaType);
HttpEntity<PermissionDHBean> httpEntity = new HttpEntity<>(permissionBean, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
log.info("发送请求,下发权限至大华设备,请求地址:{} ,返回信息:{}",url, responseEntity.getBody());
}catch (Exception e){
log.error("发送请求,下发权限至大华设备,异常信息,{}",e);
}
}
/**
* 照片下发,发送海康设备
* @param schoolId
* @param userType
* @param deviceIds
*/
public static void uploadHkImg( String schoolId,int userType,String deviceIds) {
String api = "http://114.55.30.100:8089/facereco/sendFaces";
try {
String url = String.format(api +"?schoolId=%s&userType=%s&deviceIds=%s",schoolId,userType,deviceIds);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
log.info("发送请求,照片下发至海康设备,请求地址:{},返回信息: {}",url,responseEntity.getBody());
} catch (Exception e) {
log.error("发送请求,照片下发至海康设备,异常信息:{}",e);
}
}
/**
* 照片下发,发送大华设备
* @param schoolId
* @param userType
* @param deviceIds
*/
public static void uploadDHImg(String schoolId,Integer userType,String deviceIds,Integer studentType,String sex) {
String api = "http://114.55.30.100:8991/operate/sendUserFaces";
// String api = "http://127.0.0.1:8991/user/sendFaces";
try {
String url = String.format(api +"?schoolId=%s&userType=%s&deviceIds=%s&studentType=%s&sex=%s",schoolId,userType,deviceIds,studentType,sex);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
log.info("发送请求,照片下发至大华设备,请求地址:{},返回信息: {}",url,responseEntity.getBody());
} catch (Exception e) {
log.error("发送请求,照片下发至大华设备,异常信息:{}",e);
}
}
/**
* 删除大华设备人脸
* @param schoolId
* @param cardNum
* @param deviceId
*/
public static void deleteDHFace(Integer schoolId,String cardNum,String deviceId) {
String api = "http://121.40.109.21:8991/operate/deleteFaceByCard";
try {
String url = String.format(api +"?schoolId=%s&cardNum=%s&deviceId=%s",schoolId,cardNum,deviceId);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
log.info("发送请求,删除大华设备人脸,请求地址:{},返回信息: {}",url,responseEntity.getBody());
} catch (Exception e) {
log.error("发送请求,删除大华设备人脸,异常信息:{}",e);
}
}
/**
* 在线活体检测
* @param checkUrl
* @return
*/
public static String checkFace(String checkUrl) {
String api = "http://114.55.30.100:8991/user/checkFace";
try {
String url = String.format(api +"?url=%s",checkUrl);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
log.info("发送请求,删除大华设备人脸,请求地址:{},返回信息: {}",url,responseEntity.getBody());
return responseEntity.getBody();
} catch (Exception e) {
log.error("发送请求,删除大华设备人脸,异常信息:{}",e);
}
return null;
}
}