CloseableHttpClient httpClient = HttpClientBuilder.create().
setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()).
setRedirectStrategy(new DefaultRedirectStrategy()).
setDefaultRequestConfig(requestConfig).build();
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(Lion.getStringValue("emidas-customized-web.face.recognize.url", "http://ar.sankuai.com/bonus"));
//FileBody fileBody = new FileBody(new File("C:\\Users\\Administrator\\Desktop\\517\\timg.jpg"));
InputStream ins = null;
int r = -1;
try {
//String f = file.getOriginalFilename();
ins = new URL(url).openStream();
HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("img", IOUtils.toByteArray(ins),
ContentType.DEFAULT_BINARY, System.currentTimeMillis() + ".jpg").build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = response.getEntity();
String result = EntityUtils.toString(resEntity);
// 消耗掉response
EntityUtils.consume(resEntity);
JSONObject jsonObject = JSON.parseObject(result);
if(jsonObject != null && jsonObject.getBooleanValue("success")){
JSONObject data = jsonObject.getJSONObject("data");
JSONObject face = data.getJSONObject("face");
return new FaceCutBean(url,data.getDoubleValue("value"),face.getIntValue("x")
,face.getIntValue("y"),face.getIntValue("w"),face.getIntValue("h"));
}else{
if (jsonObject != null){
r = jsonObject.getIntValue("data");
}
}
// FaceCutBean bean = new FaceCutBean();
}
} catch (Exception e) {
logger.info("getFace",e);
r = -1;
}finally {
IOUtils.closeQuietly(ins);
HttpClientUtils.closeQuietly(httpClient);
HttpClientUtils.closeQuietly(response);
}
return new FaceCutBean(r);
HttpClient 上传文件
最新推荐文章于 2024-07-17 21:01:35 发布