人脸搜索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;
}
相似问题