HttpUtils.java
3.79 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
package com.example.dahua.utils;
import com.example.dahua.bean.PermissionBean;
import com.example.dahua.lib.CompressPic;
import com.example.dahua.lib.FilePath;
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.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.io.File;
public class HttpUtils {
/**
* 单个人脸下发至设备
* 文件先由文件服务上传至100服务上,在21服务进行下发动作
* @param file
* @param schoolId
* @param studentCode
* @param userType
* @param deviceId
* @return
*/
public static boolean uploadImg(File file, Integer schoolId, String studentCode,Integer userType, String deviceId) {
if (!file.exists()) return false;
String targPath = FilePath.picPathComp + studentCode + ".jpg";
String url = "http://116.62.155.137:8991/file/uploadImg";
MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();
HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();
HttpEntity<MultiValueMap<String, Object>> httpEntity = null;
ResponseEntity<String> responseEntity = null;
try {
CompressPic.CompressPic(file.getAbsolutePath(), targPath);//压缩后的图片
MediaType mediaType = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
headers.setContentType(mediaType);
FileSystemResource fileSystemResource = new FileSystemResource(targPath);
multivaluedMap.add("file", fileSystemResource);
multivaluedMap.add("schoolId", schoolId);
multivaluedMap.add("studentCode", studentCode);
multivaluedMap.add("userType", userType);
if(!StringUtils.isEmpty(deviceId)){
multivaluedMap.add("deviceId", deviceId);
}
httpEntity = new HttpEntity<>(multivaluedMap, headers);
responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
System.out.println("responseEntity:" + responseEntity.getBody());
return responseEntity.getBody().equals("1");
} catch (Exception e) {
e.printStackTrace();
return responseEntity.getBody().equals("0");
}
}
public static void deleteFace(Integer schoolId) {
String url = "http://116.62.155.137:8991/operate/deleteFailFace?schoolId=" + schoolId;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> result = restTemplate.getForEntity(url, Boolean.class);
System.out.println("发生请求21服务删除人脸,请求地址: +" + url+ "返回信息: "+ result.getBody());
}
/**
* 权限下发至设备
* @param permissionBean
* @return
*/
public static boolean sendPermission(PermissionBean permissionBean) {
String url = "http://116.62.155.137:8991/file/sendPermission";
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);
System.out.println("大华设备下发权限:" + responseEntity.getBody());
return responseEntity.getBody().equals("1");
}
}