首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Image方法调用人脸搜索API的问题?

使用Image方法调用人脸搜索API的问题?

提问于 2018-06-05 20:41:57
回答 1关注 0查看 376

人脸搜索API提供了两种方式发送HTTp请求,我在使用image方式发送请求之后返回给我这样的结果{"code":3,"message":"invalid request: param error","data":{}}

我的代码是这样的:

public String postfile1(String url, Map<String, String> header, Map<String, String> body, String filename) throws ClientProtocolException, IOException {

HttpPost httpPost = new HttpPost(url);

if(header != null) {

Iterator httpResponse = header.keySet().iterator();

while(httpResponse.hasNext()) {

String code = (String)httpResponse.next();

System.out.println(code);

httpPost.setHeader(code, (String)header.get(code));

}

}

FileInputStream fileInputStream = new FileInputStream(filename);

System.out.println(filename);

System.out.println(fileInputStream.available());

byte[] data = new byte[fileInputStream.available()];

fileInputStream.read(data);

data = new byte[fileInputStream.available()];

System.out.println(new String(data));

ByteArrayBody bab = new ByteArrayBody(data, filename);

System.out.println(bab);

fileInputStream.close();

String appid = "1256567225";

String group_id = "tencent";

StringBody appidBody = new StringBody(appid,ContentType.MULTIPART_FORM_DATA);

System.out.println(appid.toString());

StringBody group_idBody = new StringBody(group_id,ContentType.MULTIPART_FORM_DATA);

HttpEntity reqEntity = MultipartEntityBuilder.create()

.addPart("appid", appidBody)

.addPart("group_id", group_idBody)

.addPart("image",bab).build();

httpPost.setEntity(reqEntity);

CloseableHttpClient httpClient = HttpClients.createDefault();

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity resultEntity = response.getEntity();

String responseMessage = "";

try{

System.out.println("开始解析结果");

if(resultEntity!=null){

InputStream is = resultEntity.getContent();

BufferedReader br = new BufferedReader(new InputStreamReader(is));

StringBuffer sb = new StringBuffer();

String line = "";

while((line = br.readLine()) != null){

sb.append(line);

}

responseMessage = sb.toString();

System.out.println("解析完成,解析内容为"+ responseMessage);

}

EntityUtils.consume(resultEntity);

}finally{

if (null != response){

response.close();

}

}

return responseMessage;

}

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档