HttpUtils.java
6.16 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
package com.example.dahua.utils;
import com.example.dahua.bean.PermissionBean;
import com.example.dahua.bean.PermissionFaceBean;
import com.example.dahua.bean.PermissionHKBean;
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.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class HttpUtils {
/**
* 单个人脸下发至设备
* 文件先由文件服务上传至100服务上,在21服务进行下发动作
* @param file
* @param schoolId
* @param studentCode
* @param clint_type
* @param userType
* @param deviceId
* @return
*/
public static boolean uploadImgs(File file, String schoolId, String studentCode, String clint_type, int userType,String deviceId) {
if (!file.exists()) {
System.out.println("图片不存在");
return false;
}
String targPath = FilePath.picPathComp + studentCode + ".jpg";
// String targPath = "C:\\Users\\taohandong\\Desktop\\comp\\"+userInfoBean.getStudentcode()+".jpg";
String url = "http://121.40.109.21:8991/file/uploadImg";
//String url = "http://localhost: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, studentCode);//压缩后的图片
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("clint_type", clint_type);
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 String uploadImg2HK(String filePath,String card,String name,String deviceId,String userType){
String url = String.format("http://114.55.30.100:8089/facereco/sendCardAndImg1?filePath=%s&card=%s&name=%s&userType=%s&deviceId=%s",
filePath,card,name,userType,deviceId);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
System.out.println("responseEntity:" + responseEntity.getBody());
return responseEntity.getBody();
}
public static boolean imgsSend(String schoolId, int type) {
String url = "http://121.40.109.21:8991/file/imgsSend?schoolId=" + schoolId + "&type=" + type;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> result = restTemplate.getForEntity(url, Boolean.class);
System.out.println("result:" + result.getBody());
return result.getBody();
}
/**
* 权限下发至设备
* @param permissionBean
* @return
*/
public static boolean sendPermission(PermissionBean permissionBean) {
String url = "http://121.40.109.21: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");
}
/**
* 权限下发至海康设备
* @param permissionFaceBean
*/
public static void sendPermission2HK(PermissionFaceBean permissionFaceBean) {
String url = "http://114.55.30.100:8089/facereco/sendPermiss";
PermissionBean permissionBean = permissionFaceBean.getPermissionBean();
if (null!=permissionBean){
PermissionHKBean permissionHKBean = new PermissionHKBean();
permissionHKBean.setDeviceIds(permissionBean.getDeviceIds());
permissionHKBean.setSchoolId(permissionFaceBean.getSchoolId()+"");
permissionHKBean.setUserType("2");
permissionHKBean.setWeekDays(permissionBean.getWeekDays());
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
headers.setContentType(mediaType);
HttpEntity<PermissionHKBean> httpEntity = new HttpEntity<>(permissionHKBean, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
System.out.println("海康设备下发权限:" + responseEntity.getBody());
}
}
}