WANG 5 anni fa
parent
commit
91843753a2

+ 32 - 26
src/main/java/cn/com/qmth/examcloud/web/facepp/FaceppClient.java

@@ -152,18 +152,21 @@ public class FaceppClient {
 	}
 
 	/**
-	 * 人脸识别
+	 * 人脸识别<br>
+	 * 优先使用主地址调用face++<br>
+	 * 主地址无效时,使用备用地址调用face++.<br>
+	 * 备用地址也无效时,使用备用地址下载数据,使用base64加密串调用face++<br>
 	 *
 	 * @author WANGWEI
 	 * @param faceToken
 	 * @param imageUrl
-	 *            图片url
 	 * @param backupImageUrl
-	 *            备用图片url
 	 * @return
+	 * @throws StatusException
+	 *             code为801,802,803表示图片地址无效
 	 */
 	public JsonHttpResponseHolder compareWithTokenAndImageUrl(String faceToken, String imageUrl,
-			String backupImageUrl) {
+			String backupImageUrl) throws StatusException {
 
 		JsonHttpResponseHolder responseHolder = compareWithTokenAndImageUrl(faceToken, imageUrl);
 
@@ -180,34 +183,37 @@ public class FaceppClient {
 		respBody = responseHolder.getRespBody();
 		errMsg = respBody.getString("error_message");
 
-		HttpGet get = new HttpGet(backupImageUrl);
-		get.setConfig(FaceppClient.requestConfig);
-		CloseableHttpResponse response = null;
-		String imageBase64 = null;
-		try {
-			response = httpclient.execute(get);
+		if (null != errMsg && errMsg.startsWith("INVALID_IMAGE_URL")) {
+			HttpGet get = new HttpGet(backupImageUrl);
+			get.setConfig(FaceppClient.requestConfig);
+			CloseableHttpResponse response = null;
+			String imageBase64 = null;
+			try {
+				response = httpclient.execute(get);
+
+				if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
+					throw new StatusException("801",
+							"fail to download file. url=" + backupImageUrl);
+				}
 
-			if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
-				throw new StatusException("601", "fail to download file. url=" + backupImageUrl);
-			}
+				byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
+				if (100 > byteArray.length) {
+					throw new StatusException("802", "invalid image size. url=" + backupImageUrl);
+				}
 
-			byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
-			if (100 > byteArray.length) {
-				throw new StatusException("602", "invalid image size. url=" + backupImageUrl);
-			}
+				imageBase64 = Base64.encodeBase64String(byteArray);
 
-			imageBase64 = Base64.encodeBase64String(byteArray);
+			} catch (StatusException e) {
+				throw e;
+			} catch (Exception e) {
+				throw new StatusException("803", "fail to download file. url=" + backupImageUrl, e);
+			} finally {
+				IOUtils.closeQuietly(response);
+			}
 
-		} catch (StatusException e) {
-			throw e;
-		} catch (Exception e) {
-			throw new StatusException("603", "fail to download file. url=" + backupImageUrl, e);
-		} finally {
-			IOUtils.closeQuietly(response);
+			responseHolder = compareWithTokenAndBase64(faceToken, imageBase64);
 		}
 
-		responseHolder = compareWithTokenAndBase64(faceToken, imageBase64);
-
 		return responseHolder;
 	}