|
@@ -0,0 +1,118 @@
|
|
|
|
|
+package me.zhengjie.base.util;
|
|
|
|
|
+
|
|
|
|
|
+import com.uinid.util.HttpRequestUtil;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.Base64;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+
|
|
|
|
|
+import org.apache.commons.httpclient.HttpClient;
|
|
|
|
|
+import org.apache.commons.httpclient.HttpException;
|
|
|
|
|
+import org.apache.commons.httpclient.HttpStatus;
|
|
|
|
|
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
|
|
|
|
+import org.apache.commons.httpclient.methods.PostMethod;
|
|
|
|
|
+import org.apache.commons.httpclient.params.HttpMethodParams;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+
|
|
|
|
|
+public class JuheServiceUtil {
|
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(HttpRequestUtil.class);
|
|
|
|
|
+
|
|
|
|
|
+ public static final String UTF8 = "UTF-8";
|
|
|
|
|
+ private static final String EMPTY = "";
|
|
|
|
|
+ private static final String JuHeURl = "http://apis.juhe.cn/verifyface/verify";
|
|
|
|
|
+
|
|
|
|
|
+ private static MultiThreadedHttpConnectionManager connectionManager = null;
|
|
|
|
|
+
|
|
|
|
|
+ private static int connectionTimeOut = 5000;
|
|
|
|
|
+
|
|
|
|
|
+ private static int socketTimeOut = 5000;
|
|
|
|
|
+
|
|
|
|
|
+ private static int maxConnectionPerHost = 500;
|
|
|
|
|
+
|
|
|
|
|
+ private static int maxTotalConnections = 500;
|
|
|
|
|
+
|
|
|
|
|
+ private static HttpClient client;
|
|
|
|
|
+
|
|
|
|
|
+ static {
|
|
|
|
|
+ connectionManager = new MultiThreadedHttpConnectionManager();
|
|
|
|
|
+ connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
|
|
|
|
|
+ connectionManager.getParams().setSoTimeout(socketTimeOut);
|
|
|
|
|
+ connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
|
|
|
|
|
+ connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
|
|
|
|
|
+ client = new HttpClient(connectionManager);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 聚合人脸比对接口
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param url 请求地址
|
|
|
|
|
+ * @param params 业务参数
|
|
|
|
|
+ * @return String response
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String URLPost(String url, Map<String, Object> params) {
|
|
|
|
|
+ String response = EMPTY;
|
|
|
|
|
+ PostMethod postMethod = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ postMethod = new PostMethod(url);
|
|
|
|
|
+ // 将表单的值放入postMethod中
|
|
|
|
|
+ if (params != null) {
|
|
|
|
|
+ Set<String> keySet = params.keySet();
|
|
|
|
|
+ for (String key : keySet) {
|
|
|
|
|
+ Object value = params.get(key);
|
|
|
|
|
+ postMethod.addParameter(key, String.valueOf(value));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, UTF8);
|
|
|
|
|
+ // 执行postMethod
|
|
|
|
|
+ int statusCode = client.executeMethod(postMethod);
|
|
|
|
|
+ if (statusCode == HttpStatus.SC_OK) {
|
|
|
|
|
+ response = postMethod.getResponseBodyAsString();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.error("响应状态码=" + postMethod.getStatusCode());
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (HttpException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (postMethod != null) {
|
|
|
|
|
+ postMethod.releaseConnection();
|
|
|
|
|
+ postMethod = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
|
|
+ String imgFile = "";// 图片地址
|
|
|
|
|
+ File file = new File(imgFile);
|
|
|
|
|
+ String realName = "xxx";// 姓名
|
|
|
|
|
+ String idCard = "xxxxxxxxxxxxxxxx";// 身份证号
|
|
|
|
|
+ FileInputStream fin = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ fin = new FileInputStream(file);
|
|
|
|
|
+ byte[] bytes = new byte[fin.available()];
|
|
|
|
|
+ fin.read(bytes);
|
|
|
|
|
+ String base64 = Base64.getEncoder().encodeToString(bytes);
|
|
|
|
|
+ System.out.println("=====base64" + base64);
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("idcard", idCard);
|
|
|
|
|
+ params.put("realname", realName);
|
|
|
|
|
+ params.put("image", base64);
|
|
|
|
|
+ params.put("key", "6db21a71bee88af1eac38480c1574e2a");
|
|
|
|
|
+ String data = JuheServiceUtil.URLPost(JuHeURl, params);
|
|
|
|
|
+ System.out.println(data);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (fin != null) {
|
|
|
|
|
+ fin.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|