@ResponseBody
@RequestMapping(value = "/checkSame", method = RequestMethod.POST)
public ApiResult checkSame(@RequestParam(value = "imageUrl", required = false) MultipartFile imageUrl) {
ApiResult result = new ApiResult();
try {
SimilarResult sr = cloudUtil.similarCloud(encode(imageUrl.getBytes()));
List<Results> list = sr.getSimilar().getResults();
if(list.size() == 0){
result.msg.status = 0;
result.msg.desc = "暂无相似图片";
}else{
String a = sr.getSimilar().getResults().get(0).getTargetId();
cloudUtil.getCloud(a);
result.msg.status = 1;
result.msg.desc = "已存在相似图片,请重新上传";
}
} catch (Exception e) {
result.msg.status = 3;
result.msg.desc = "系统问题,操作失败";
}
return result;
}
static SimilarResult sr = new SimilarResult();
public static SimilarResult similarCloud(String file) throws Exception {
AsyncHttpClient client = new DefaultAsyncHttpClient();
JSONObject params = new JSONObject();
params.put("image", file);
Auth.signParam(params, APP_KEY, APP_SECRET);
client.preparePost(HOST + "/similar/")
.setBody(params.toString().getBytes())
.execute(new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
System.out.println(response.getResponseBody());
Gson gson = new Gson();
sr = gson.fromJson(response.getResponseBody(),SimilarResult.class);
client.close();
return null;
}
});
return sr;
}