|
@@ -1,7 +1,11 @@
|
|
package cn.com.qmth.examcloud.starters.face.verify.common;
|
|
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.codec.binary.Base64;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -19,14 +23,19 @@ public class CommonUtils {
|
|
private final static Logger log = LoggerFactory.getLogger(CommonUtils.class);
|
|
private final static Logger log = LoggerFactory.getLogger(CommonUtils.class);
|
|
|
|
|
|
public static String toBase64(File imageFile) {
|
|
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("图片文件不存在!");
|
|
throw new IllegalArgumentException("图片文件不存在!");
|
|
}
|
|
}
|
|
|
|
|
|
String regex = "\\.(?:jpg|jpeg|png)$";
|
|
String regex = "\\.(?:jpg|jpeg|png)$";
|
|
Matcher matcher = Pattern.compile(regex).matcher(imageFile.getName().toLowerCase());
|
|
Matcher matcher = Pattern.compile(regex).matcher(imageFile.getName().toLowerCase());
|
|
if (!matcher.find()) {
|
|
if (!matcher.find()) {
|
|
- throw new IllegalArgumentException("图片文件格式不正确,仅支持(jpg、png)!");
|
|
|
|
|
|
+ throw new IllegalArgumentException("图片文件格式不正确,仅支持jpg、png!");
|
|
}
|
|
}
|
|
|
|
|
|
try (InputStream is = Files.newInputStream(imageFile.toPath());) {
|
|
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) {
|
|
public static String urlEncode(String value) {
|
|
- if (value == null) {
|
|
|
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
|
|
|