/**
* 人脸检索 byte模式
* @param image
* @param groupIds
* @return
* @throws AbstractImageException
* @throws IOException
*/
private QcloudFaceResult<IdentifyResult> faceIdentifyByte( byte[] image, String[] groupIds) throws AbstractImageException, IOException {
FaceIdentifyRequest request = new FaceIdentifyRequest(CosBucketNameConst.bucketName, groupIds, image);
String ret = imageClient.faceIdentify(request);
return JsonUtil.json2pojo(ret, QcloudFaceResult.class, IdentifyResult.class);
}
public String faceIdentify(FaceIdentifyRequest request) throws AbstractImageException {
request.check_param();
String sign = Sign.appSign(this.cred, request.getBucketName(), (long)this.config.getSignExpired());
String url = "https://" + this.config.getQCloudImageDomain() + this.config.getFaceIdentify();
HttpRequest httpRequest = new HttpRequest();
httpRequest.setUrl(url);
httpRequest.addHeader("Authorization", sign);
httpRequest.addHeader("User-Agent", this.config.getUserAgent());
httpRequest.addParam("appid", String.valueOf(this.cred.getAppId()));
httpRequest.addParam("bucket", request.getBucketName());
httpRequest.setMethod(HttpMethod.POST);
String groupId = request.getGroupId();
String[] groupIds = request.getGroupIds();
if (request.isUrl()) { //对url进行
httpRequest.addHeader("Content-Type", String.valueOf(HttpContentType.APPLICATION_JSON));
httpRequest.addParam("url", request.getUrl());
httpRequest.setContentType(HttpContentType.APPLICATION_JSON);
if (groupId != null && !groupId.isEmpty()) {
httpRequest.addParam("group_id", groupId);
} else {
if (groupIds == null || groupIds.length <= 0) {
throw new ParamException("groupId and groupIds both null or empty!!");
}
httpRequest.addParam("group_ids", request.getGroupIds());
}
} else {
httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
httpRequest.addFile("image", request.getImage()); //这个位置不管传入的是byte还是file都addFile,若注入的是byte[]参数会引发后面一个错误
BytesContent bytesContent = request.getBytesContent();
if (bytesContent != null) {
httpRequest.addBytes(bytesContent.getKey(), bytesContent.getContent());
}
if (groupId != null && !groupId.isEmpty()) {
httpRequest.addParam("group_id", groupId);
} else {
if (groupIds == null || groupIds.length <= 0) {
throw new ParamException("groupId and groupIds both null or empty!!");
}
for(int index = 0; index < groupIds.length; ++index) {
String key = String.format("group_ids[%d]", index);
String data = groupIds[index];
httpRequest.addParam(key, data);
}
}
}
return this.httpClient.sendHttpRequest(httpRequest);
}
public String sendHttpRequest(HttpRequest httpRequest) throws AbstractImageException {
HttpMethod method = httpRequest.getMethod();
if (method == HttpMethod.POST) {
return sendPostRequest(httpRequest);//进入这个方法
} else {
throw new ParamException("Unsupported Http Method");
}
}
if (file == null) {
throw new FileNotFoundException("File is null: " + key);
}
private void setMultiPartEntity(MultipartBuilder multipartBuilder, Map<String, Object> params, Map<String, File> files, Map<String, byte[]> fileContents)
throws FileNotFoundException {
multipartBuilder.type(MultipartBuilder.FORM);
for (String paramKey : params.keySet()) {
multipartBuilder.addFormDataPart(paramKey, String.valueOf(params.get(paramKey)));
}
if (files.size() > 0) {
for (String key : files.keySet()) {
File file = files.get(key);
if (file == null) {
throw new FileNotFoundException("File is null: " + key);
}
if (!file.exists()) {
throw new FileNotFoundException("File Not Exists: " + file.getAbsolutePath());
}
multipartBuilder.addPart(
Headers.of("Content-Disposition", String.format("form-data; name=\"%s\"; filename=\"%s\"", key, file.getName())),
RequestBody.create(MediaType.parse("image/jpg"), file)
);
}
}
for (String key : fileContents.keySet()) {
byte[] content = fileContents.get(key);
if (content != null && content.length > 0) {
multipartBuilder.addPart(
//TODO file name not resolved
Headers.of("Content-Disposition", String.format("form-data; name=\"%s\"; filename=\"%s\"", key, "bytes" + System.currentTimeMillis())),
RequestBody.create(MediaType.parse("image/jpg"), content)
);
}
}
}