deason 2 жил өмнө
parent
commit
f4e18acaec

+ 0 - 22
examcloud-starters/examcloud-face-verify-starter/src/main/java/cn/com/qmth/examcloud/starters/face/verify/common/ApiLimitException.java

@@ -1,22 +0,0 @@
-package cn.com.qmth.examcloud.starters.face.verify.common;
-
-public class ApiLimitException extends RuntimeException {
-
-    private String desc;
-
-    public ApiLimitException(String desc) {
-        super(desc);
-        this.desc = desc;
-    }
-
-    public ApiLimitException(String desc, Throwable e) {
-        super(desc, e);
-        this.desc = desc;
-    }
-
-    @Override
-    public String toString() {
-        return desc;
-    }
-
-}

+ 33 - 3
examcloud-starters/examcloud-face-verify-starter/src/main/java/cn/com/qmth/examcloud/starters/face/verify/common/CommonUtils.java

@@ -1,7 +1,11 @@
 package cn.com.qmth.examcloud.starters.face.verify.common;
 
+import okhttp3.Request;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -19,14 +23,19 @@ public class CommonUtils {
     private final static Logger log = LoggerFactory.getLogger(CommonUtils.class);
 
     public static String toBase64(File imageFile) {
-        if (imageFile == null || !imageFile.exists()) {
+        if (imageFile == null) {
+            throw new IllegalArgumentException("图片文件参数值不能为空!");
+        }
+
+        if (!imageFile.exists()) {
+            log.error("imageFile is not exist, path = {}", imageFile.getAbsolutePath());
             throw new IllegalArgumentException("图片文件不存在!");
         }
 
         String regex = "\\.(?:jpg|jpeg|png)$";
         Matcher matcher = Pattern.compile(regex).matcher(imageFile.getName().toLowerCase());
         if (!matcher.find()) {
-            throw new IllegalArgumentException("图片文件格式不正确,仅支持jpg、png!");
+            throw new IllegalArgumentException("图片文件格式不正确,仅支持jpg、png!");
         }
 
         try (InputStream is = Files.newInputStream(imageFile.toPath());) {
@@ -38,8 +47,29 @@ public class CommonUtils {
         }
     }
 
+    public static String toBase64(String imageUrl) {
+        if (StringUtils.isEmpty(imageUrl)) {
+            throw new IllegalArgumentException("图片地址参数值不能为空!");
+        }
+
+        Request request = new Request.Builder().url(imageUrl).get().build();
+        try (Response response = HttpClientBuilder.getClient().newCall(request).execute();) {
+            if (response.isSuccessful()) {
+                ResponseBody body = response.body();
+                if (body != null) {
+                    return Base64.encodeBase64String(body.bytes());
+                }
+            }
+            log.error("imageUrl download fail {}", imageUrl);
+        } catch (Exception e) {
+            log.error("imageUrl download error {} {}", imageUrl, e.getMessage(), e);
+        }
+
+        throw new RuntimeException("图片加载失败!");
+    }
+
     public static String urlEncode(String value) {
-        if (value == null) {
+        if (StringUtils.isEmpty(value)) {
             return "";
         }
 

+ 13 - 0
examcloud-starters/examcloud-face-verify-starter/src/main/java/cn/com/qmth/examcloud/starters/face/verify/common/FaceVerifyException.java

@@ -0,0 +1,13 @@
+package cn.com.qmth.examcloud.starters.face.verify.common;
+
+public class FaceVerifyException extends RuntimeException {
+
+    public FaceVerifyException(String msg) {
+        super(msg);
+    }
+
+    public FaceVerifyException(String msg, Throwable e) {
+        super(msg, e);
+    }
+
+}

+ 0 - 11
examcloud-starters/examcloud-face-verify-starter/src/main/java/cn/com/qmth/examcloud/starters/face/verify/common/JsonHelper.java

@@ -1,6 +1,5 @@
 package cn.com.qmth.examcloud.starters.face.verify.common;
 
-import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
@@ -11,7 +10,6 @@ import org.slf4j.LoggerFactory;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 
-@SuppressWarnings("unchecked")
 public class JsonHelper {
 
     private static Logger log = LoggerFactory.getLogger(JsonHelper.class);
@@ -19,17 +17,8 @@ public class JsonHelper {
     private ObjectMapper mapper;
 
     public JsonHelper() {
-        this(null);
-    }
-
-    public JsonHelper(Include include) {
         mapper = new ObjectMapper();
 
-        //设置输出时包含属性的风格
-        if (include != null) {
-            mapper.setSerializationInclusion(include);
-        }
-
         //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);