refactor(): 人脸识别功能补充异常排查与权限判断(增加了nacos请求内网图片配置)解决SSL证书问题

main
lishiwei 2026-08-03 17:03:22 +08:00
parent 20a5a3a4fc
commit 6ae851adf6
2 changed files with 183 additions and 116 deletions

View File

@ -57,16 +57,16 @@ public class StudentSignAddExe {
String faceUrl = null;
try {
faceUrl = Base64Util.getBase64String(studentSignE.getFiles()[0]);
log.info("StudentSignAddExe-execute,调用阿里进行人脸识别开始");
long startAliTime = System.currentTimeMillis();
Boolean environment = false;
log.info("StudentSignAddExe-execute,fileUrlConfig.getPrefixUrl():{}",fileUrlConfig.getPrefixUrl());
if (!"https://jpfz.qhdsafety.com/gbsFileTest/".equals(fileUrlConfig.getPrefixUrl())) {
environment = true;
}
log.info("StudentSignAddExe-execute,开始进行人脸识别,prefixUrl:{},environment:{}", fileUrlConfig.getPrefixUrl(), environment);
long startAliTime = System.currentTimeMillis();
studentSignE.compareFace(student.getUserAvatarUrl(), faceUrl, fileUrlConfig.getPrefixUrl(), environment);
log.info("StudentSignAddExe-execute,调用阿里进行人脸识别结束,耗时:{}",System.currentTimeMillis() - startAliTime);
log.info("StudentSignAddExe-execute,人脸识别结束,耗时:{}", System.currentTimeMillis() - startAliTime);
} catch (BizException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -75,7 +75,12 @@ public class StudentSignAddExe {
String fileName = Tools.get32UUID() + studentSignE.getFiles()[0].getOriginalFilename().substring(studentSignE.getFiles()[0].getOriginalFilename().lastIndexOf("."));
log.info("StudentSignAddExe-execute,保存人脸图片开始");
long l = System.currentTimeMillis();
studentSignE.setFaceUrl(zcloudImgFilesFacade.saveFile( faceUrl,fileName, path));
try {
studentSignE.setFaceUrl(zcloudImgFilesFacade.saveFile(
studentSignE.getFiles()[0].getBytes(), fileName, path, stu.getClassCorpinfoId()));
} catch (IOException e) {
throw new BizException("保存人脸图片失败");
}
log.info("StudentSignAddExe-execute,保存人脸图片结束,耗时:{}",System.currentTimeMillis() - l);
studentSignE.init(stu.getStudentId(), stu.getClassId(), stu.getClassCorpinfoId());
boolean res = false;

View File

@ -6,11 +6,17 @@ import com.alibaba.fastjson.JSONObject;
import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.gbscommon.utils.Base64Util;
import com.zcloud.gbscommon.utils.FaceUtil;
import com.zcloud.gbscommon.utils.HttpRequestUtil;
import com.zcloud.gbscommon.utils.Tools;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@ -71,19 +77,16 @@ public class StudentSignE extends BaseE {
public void compareFace(String templateFaceUrl, String faceUrl, String prefixUrl, Boolean environment) throws Exception {
try {
String templateFace = Base64Util.urlToBase64(prefixUrl + templateFaceUrl);
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic1", templateFace);
map.put("pic2", faceUrl);
String jsonParam = JSON.toJSONString(map);
String compareResultStr;
if (environment) {
// 正式环境
String compareResult = HttpRequestUtil.doPost("http://192.168.192.201:8971/gbs-face/face/compareFace", jsonParam);
JSONObject jsonObject = JSONObject.parseObject(compareResult);
jsonObject.getJSONObject("info").getString("confidence");
compareResultStr = jsonObject.getJSONObject("info").getString("confidence");
// 正式环境gbs-face
Map<String, Object> map = new HashMap<>();
map.put("pic1", templateFace);
map.put("pic2", faceUrl);
String compareResult = callGbsFaceCompare(JSON.toJSONString(map));
compareResultStr = parseGbsFaceConfidence(compareResult);
} else {
// 测试环境
// 测试环境:阿里云
compareResultStr = FaceUtil.compareFace(templateFace, faceUrl);
}
@ -91,11 +94,70 @@ public class StudentSignE extends BaseE {
throw new BizException("人脸不匹配");
}
this.setStudentSignId(Tools.get32UUID());
} catch (BizException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
throw new BizException("人脸不匹配");
throw new BizException("人脸比对失败: " + e.getMessage());
}
}
private String callGbsFaceCompare(String jsonParam) throws Exception {
URL url = new URL("http://192.168.192.201:8971/gbs-face/face/compareFace");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
byte[] body = jsonParam.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(body.length);
try (OutputStream os = connection.getOutputStream()) {
os.write(body);
}
int responseCode = connection.getResponseCode();
InputStream is = responseCode >= 200 && responseCode < 300
? connection.getInputStream() : connection.getErrorStream();
if (is == null) {
return "";
}
StringBuilder result = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
} finally {
connection.disconnect();
}
return result.toString();
}
private String parseGbsFaceConfidence(String compareResult) {
if (compareResult == null || compareResult.trim().isEmpty()) {
throw new BizException("人脸比对服务无响应");
}
JSONObject jsonObject = JSONObject.parseObject(compareResult);
if (jsonObject == null) {
throw new BizException("人脸比对服务返回异常: " + compareResult);
}
if ("exception".equals(jsonObject.getString("result"))) {
String exceptionMsg = jsonObject.getString("exception");
throw new BizException(exceptionMsg != null ? exceptionMsg : "人脸比对服务异常");
}
JSONObject info = jsonObject.getJSONObject("info");
if (info != null && info.getString("confidence") != null) {
return info.getString("confidence");
}
JSONObject data = jsonObject.getJSONObject("data");
if (data != null && data.getString("confidence") != null) {
return data.getString("confidence");
}
if (jsonObject.getString("confidence") != null) {
return jsonObject.getString("confidence");
}
throw new BizException("人脸比对服务返回异常: " + compareResult);
}
public void init(String studentId, String classId, long corpinfoId) {