QQ小程序图片内容安全检测
在网上看到一篇微信小程序的图片检测以为QQ也一样,但是一直报参数错误,
其实qq小程序是抄xi的有的问题,api抄的一模一样(懒鬼),比如微信是xxcode,
qq返回的key是xxCode,大小写,cao,还要吐槽一下垃圾csdn富文本编辑器,=》lj
微信的实现 https://www.jianshu.com/p/d40c223ce29d
qq应该这样实现
//核心代码
Map<String, ContentBody> reqParam = new HashMap<>();
reqParam.put("media",new InputStreamBody(inputStream,"img.jpg"));
return HttpClientUtil.doPostFileMultiPart(requestUrl, reqParam);
//关键使用的封装
public static String doPostFileMultiPart(String url,Map<String,ContentBody> reqParam)throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpPost httppost = new HttpPost(url);
//setConnectTimeout:设置连接超时时间,单位毫秒。setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(15000).build();
httppost.setConfig(defaultRequestConfig);
// System.out.println("executing request " + httppost.getURI());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (Map.Entry<String, ContentBody> param : reqParam.entrySet()) {
multipartEntityBuilder.addPart(param.getKey(), param.getValue());
}
HttpEntity reqEntity = multipartEntityBuilder.build();
httppost.setEntity(reqEntity);
// 执行post请求.
CloseableHttpResponse response = httpclient.execute(httppost);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
//System.out.println("--------------------------------------");
// 打印响应状态
//System.out.println(response.getStatusLine());
if (entity != null) {
return EntityUtils.toString(entity, Charset.forName("UTF-8"));
}
//System.out.println("------------------------------------");
} finally {
response.close();
}
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}