Commit 7e1925ac by liulianglang

比对

parent eccba21e
......@@ -22,6 +22,7 @@ import org.springblade.founder.ryxscxbd.mapper.TbYwRxbdBdzgMapper;
import org.springblade.founder.ryxscxbd.service.RxbzService;
import org.springblade.founder.ryxscxbd.service.TbYwRxbdBdzgService;
import org.springblade.founder.ryxscxbd.utils.HttpClient;
import org.springblade.founder.ryxscxbd.utils.HttpClientService;
import org.springblade.founder.ryxscxbd.utils.HttpClientUtil;
import org.springblade.founder.ryxscxbd.utils.RxbdUtils;
import org.springblade.founder.utils.R;
......@@ -60,6 +61,10 @@ public class TbYwRxbdBdzgServiceImpl implements TbYwRxbdBdzgService {
private RxbzService rxbzService;
@Autowired
private RxbzMapper rxbzMapper;
@Autowired
private RxbdUtils rxbdUtils;
@Autowired
private HttpClientService httpClientService;
//1、获取token
private static final String HAINANRX_GETTOKEN_URL = "http://74.10.26.200:10219/uums/auth/token";
//4、人像检索
......@@ -477,9 +482,14 @@ public class TbYwRxbdBdzgServiceImpl implements TbYwRxbdBdzgService {
try {
taskId = upload("http://68.174.25.90:9101/feiShi/sendFeiShiTask", file, threshold, topNumber);
// taskId = upload("http://68.174.25.90:9101/feiShi/sendFeiShiTask", file, threshold, topNumber);
// String taskId = upload("http://68.175.7.113:8034/feiShi/sendFeiShiTask",path,threshold,topNumber);
// taskId = UUID.randomUUID().toString();
String url = rxbdUtils.getTaskUrl();
Map<String, String> params = rxbdUtils.getTaskParam();
taskId= httpClientService.doPostFile(url, params, file, "");
System.out.println("taskId=====" + taskId);
} catch (Exception e) {
......@@ -614,32 +624,13 @@ public class TbYwRxbdBdzgServiceImpl implements TbYwRxbdBdzgService {
return R.ok().data(map);
}
public static String sendTask(String taskId) {
String url = "http://68.174.25.90:9101/feiShi/getFeiShiResult?taskId=" + taskId + "&userId=150147";
System.out.println("===================" + url);
String result = "";
String urlName = url;
try {
URL realURL = new URL(urlName);
URLConnection conn = realURL.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
conn.connect();
Map<String, List<String>> map = conn.getHeaderFields();
// for (String s : map.keySet()) {
// System.out.println(s + "-->" + map.get(s));
// }
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("result===================" + url);
return result;
public String sendTask(String taskId) {
String url = rxbdUtils.getResultUrl();
Map<String, String> params = rxbdUtils.getResultParam(taskId);
log.info("人像比对开始获取结果,taskId为:{},请求地址为:{},请求参数为:{}", taskId, url, params);
String response = httpClientService.doGet(url, params);
return response;
}
private static String MulFileToBase64(MultipartFile file) throws IOException {
......
package org.springblade.founder.ryxscxbd.utils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class HttpClientService {
@Autowired
private CloseableHttpClient httpClient;
@Autowired
private RequestConfig requestConfig;
public String doGet(String url, Map<String, String> params, String charset) {
String result = null;
//1.判断字符集编码是否为空 如果为空则给定默认值utf-8
if (StringUtils.isEmpty(charset)) {
charset = "UTF-8";
}
//2.判断用户是否需要传递参数
if (params != null) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
//url?id=1&name=tom
url = uriBuilder.build().toString();
} catch (Exception e) {
e.printStackTrace();
}
}
//3.定义参数提交对象
HttpGet get = new HttpGet(url);
//4.为请求设定超时时间
get.setConfig(requestConfig);
//5.通过httpClient发送请求
try {
CloseableHttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
//表示程序调用成功
result = EntityUtils.toString(response.getEntity(), charset);
} else {
System.out.println("调用异常:状态信息:" + response.getStatusLine().getStatusCode());
throw new RuntimeException();
}
response.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String doGet(String url) {
return doGet(url, null, null);
}
public String doGet(String url, Map<String, String> params) {
return doGet(url, params, null);
}
public String doGet(String url, String charset) {
return doGet(url, null, charset);
}
//实现httpClient POST提交
public String doPost(String url, Map<String, String> params, String charset) {
String result = null;
//1.定义请求类型
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig); //定义超时时间
//2.判断字符集是否为null
if (StringUtils.isEmpty(charset)) {
charset = "UTF-8";
}
//3.判断用户是否传递参数
if (params != null) {
//3.2准备List集合信息
List<NameValuePair> parameters =
new ArrayList<>();
//3.3将数据封装到List集合中
for (Map.Entry<String, String> entry : params.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
//3.1模拟表单提交
try {
UrlEncodedFormEntity formEntity =
new UrlEncodedFormEntity(parameters, charset); //采用u8编码
//3.4将实体对象封装到请求对象中
post.setEntity(formEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
//4.发送请求
try {
CloseableHttpResponse response = httpClient.execute(post);
//4.1判断返回值状态
if (response.getStatusLine().getStatusCode() == 200) {
//4.2表示请求成功
result = EntityUtils.toString(response.getEntity(), charset);
} else {
System.out.println("获取状态码信息:" + response.getStatusLine().getStatusCode());
throw new RuntimeException();
}
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public String doPostFile(String url, Map<String, String> params, MultipartFile file, String charset) {
String result = null;
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig); //定义超时时间
if (StringUtils.isEmpty(charset)) {
charset = "UTF-8";
}
//判断用户是否传递参数
if (params != null && file != null) {
try {
//MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setContentType(ContentType.MULTIPART_FORM_DATA);
/*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getOriginalFilename());
ContentType contentType = ContentType.create("text/plain", charset);
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.addTextBody(entry.getKey(), entry.getValue(), contentType);
}
HttpEntity entity = builder.build();
post.setEntity(entity);
} catch (Exception e) {
e.printStackTrace();
}
}
//4.发送请求
try {
CloseableHttpResponse response = httpClient.execute(post);
//4.1判断返回值状态
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//4.2表示请求成功
result = EntityUtils.toString(response.getEntity(), charset);
} else {
System.out.println("获取状态码信息:" + response.getStatusLine().getStatusCode());
throw new RuntimeException();
}
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public String doPostJson(String url, Map<String, String> param, String token, String charset) {
String result = null;
//1.定义请求类型
HttpPost post = new HttpPost(url);
post.setHeader("token", "");
if (!StringUtils.isEmpty(token)) {
post.setHeader("token", token);
}
post.setConfig(requestConfig); //定义超时时间
//2.判断字符集是否为null
if (StringUtils.isEmpty(charset)) {
charset = "UTF-8";
}
//3.判断用户是否传递参数
if (param != null) {
StringEntity s = new StringEntity(JSON.toJSONString(param), charset);
post.setEntity(s);
}
//4.发送请求
try {
CloseableHttpResponse response = httpClient.execute(post);
//4.1判断返回值状态
if (response.getStatusLine().getStatusCode() == 200) {
//4.2表示请求成功
result = EntityUtils.toString(response.getEntity(), charset);
} else {
System.out.println("获取状态码信息:" + response.getStatusLine().getStatusCode());
throw new RuntimeException();
}
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//请求webservice
public String doWsdl(String url, String param, String charset) {
String result = "";
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
if (StringUtils.isEmpty(charset)) {
charset = "UTF-8";
}
if (!StringUtils.isEmpty(param)) {
StringEntity data = new StringEntity(param, charset);
post.setEntity(data);
}
try {
CloseableHttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), charset);
} else {
System.out.println("获取状态码信息:" + response.getStatusLine().getStatusCode());
throw new RuntimeException();
}
response.close();
} catch (IOException e) {
log.info("外部接口访问异常:" + e.getMessage() + ",请求地址为:" + url + ",请求参数为:" + param);
//result = e.getMessage();
}
return result;
}
public String doPost(String url) {
return doPost(url, null, null);
}
public String doPost(String url, Map<String, String> params) {
return doPost(url, params, null);
}
public String doPost(String url, String charset) {
return doPost(url, null, charset);
}
}
package org.springblade.founder.ryxscxbd.utils;
/**
* @author zzj
* 人像比对发起请求常量类
*/
public class RxbdContants {
/**
* 人像比对获取地址
*/
public static final String REQUEST_IPS = "68.174.69.119,68.174.25.90,68.174.25.95";
public static final String REQUEST_URL = "68.174.25.90";
/**
* 人像比对获取taskId访问地址
*/
public static final String RXBD_TASK_URL = ":9101/feiShi/sendFeiShiTask";
/**
* 人像比对获取task结果访问地址
*/
public static final String RXBD_RESULT_URL = ":9101/feiShi/getFeiShiResult";
/**
* 人像比对请求参数
*/
public static final String RXBD_PARAM_TOPNUMBER = "10";
/**
* 人像比对请求参数
*/
//public static final String RXBD_PARAM_USERID = "150147";
public static final String RXBD_PARAM_USERID = "281851";
public static final String RXBD_PARAM_USERIDCARD = "441900198804262777";
public static final String RXBD_PARAM_USERNAME = "陈建昌";
/**
* 人像比对请求参数
*/
public static final String RXBD_PARAM_THRESHOLD = "90";
/**
* 人像比对解析task结果Key名
*/
public static final String RXBD_PARSE_TASKID = "taskId";
}
......@@ -47,6 +47,8 @@ public class RxbdUtils {
ThreadLocal<String> requestTL = new ThreadLocal<>();
public static JSONObject getPosition(Map<String, String> headers, JSONObject objectdw){
JSONObject objPos = new JSONObject(new LinkedHashMap());
......@@ -271,7 +273,71 @@ public class RxbdUtils {
}
/**
* 获取taskId参数
*
* @return
*/
public Map<String, String> getTaskParam() {
Map<String, String> params = new HashMap<>(16);
params.put("threshold", RxbdContants.RXBD_PARAM_THRESHOLD);
params.put("topNumber", RxbdContants.RXBD_PARAM_TOPNUMBER);
params.put("userId", RxbdContants.RXBD_PARAM_USERID);
params.put("userIdNumber", RxbdContants.RXBD_PARAM_USERIDCARD);
params.put("userName", RxbdContants.RXBD_PARAM_USERNAME);
params.put("userIp", getRxbdRequestIp());
return params;
}
/**
* 获取task结果参数
*
* @return
*/
public Map<String, String> getResultParam(String taskId) {
Map<String, String> params = new HashMap<>(16);
params.put("taskId", taskId);
params.put("userId", RxbdContants.RXBD_PARAM_USERID);
params.put("userIdNumber", RxbdContants.RXBD_PARAM_USERIDCARD);
params.put("userName", RxbdContants.RXBD_PARAM_USERNAME);
params.put("userIp", getRxbdRequestIp());
return params;
}
public String getResultUrl() {
// String requestIp = requestTL.get();
String requestIp = RxbdContants.REQUEST_URL;
StringBuilder sb = new StringBuilder();
sb.append("http://").append(requestIp).append(RxbdContants.RXBD_RESULT_URL);
return sb.toString();
//return "http://localhost:8065/rxbd/getFakeTaskResult";
}
public String getTaskUrl() {
// String requestIp = getRxbdRequestIp();
// requestTL.set(requestIp);
String requestIp = RxbdContants.REQUEST_URL;
StringBuilder sb = new StringBuilder();
sb.append("http://").append(requestIp).append(RxbdContants.RXBD_TASK_URL);
return sb.toString();
//return "http://localhost:8065/rxbd/getFakeTaskId";
}
/**
* 获取请求ip
*
* @return
*/
public String getRxbdRequestIp() {
String randomIp = requestTL.get();
if (StringUtils.isEmpty(randomIp)) {
int randomIndex = new Random().nextInt(3);
randomIp = RxbdContants.REQUEST_IPS.split(",")[randomIndex];
requestTL.set(randomIp);
}
return randomIp;
}
/**
* 自动调节精度(经验数值)
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment