public Map<String, Object> createFace(Map map) {
logger.info("新增G3人脸 map={}", map);
String url =bash_url +create_face_url;
File imageFile =null;
Map response;
try {
HttpHeaders headers = getHttpHeaders(map);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String,Object> params =new LinkedMultiValueMap<>();
params.add("name", map.get("name"));
params.add("group", map.get("group"));
params.add("phone", map.get("phone"));
params.add("type", map.get("type"));
imageFile = getFileByUrl((String) map.get("avatar"), "jpg");
FileSystemResource resource =new FileSystemResource(imageFile);
params.add("avatar", resource);
params.add("section", map.get("section"));
if ("2".equals(map.get("type"))) {
params.add("enabledAt", map.get("enabledAt"));
params.add("disabledAt", map.get("disabledAt"));
}
HttpEntity<MultiValueMap<String,Object>> request =new HttpEntity<>(params, headers);
String body =restTemplateComponent.requestForm(url, request);
response =objectMapper.readValue(body, Map.class);
if (response ==null) throw new HttpServiceRuntimeException(HttpStatus.BAD_REQUEST, "远程调用失败", "新增G3人脸失败");
logger.info("新增G3人脸成功");
return response;
} catch (Exception e) {
logger.error("新增G3人脸异常", e);
throw new HttpServiceRuntimeException(HttpStatus.BAD_REQUEST, "新增G3人脸异常", e.getMessage());
} finally {
if (imageFile !=null && imageFile.exists()) {
imageFile.delete();
logger.info("成功删除临时文件 file={}", imageFile);
}
}
}
public String requestForm(String url, HttpEntity<MultiValueMap<String,Object>> request) {
logger.info("request_form_url={}", url);
ResponseEntity<String> responseEntity =restTemplate.postForEntity( url, request, String.class);
Boolean isSuccessful = responseEntity.getStatusCode().is2xxSuccessful();
logger.info("isSuccessful_form={}", isSuccessful);
return isSuccessful ? responseEntity.getBody() :null;
}