最近在搞论坛 论坛可以发图片和文字,所以要保证图片文字的安全性
对于文字校验 之前发过一篇DFA文字校验的 https://blog.csdn.net/java_ying/article/details/102902525
这篇主要记录阿里与图片校验的使用
其实使用步骤阿里云官网已经介绍的很完全了(认真脸,可能是实习生写的文档吧,唉)
sdk写的也很棒(哪个人写的,出来我绝对不打死你)
1、首先你要有个阿里云帐号
2、然后要创建子账号 拿到
accessKeyId=xxxx
accessKeySecret=xxx
这么两个宝贵的东西
3、开通内容管理的服务,不要怕,开通不要钱。好像是30天内免费使用
4、给你的帐号添加个AliyunYundunGreenWebFullAccess权限
在右上角访问控制里面
5、可以开始开发了
重新排号
1、首先你要有个maven工程
2、然后你要有个配置文件,名为config,properties
#阿里云accessKeyId、accessKeySecret
accessKeyId=xxx
accessKeySecret=xx
#调用阿里绿网服务的regionId,支持cn-shanghai
regionId=cn-beijing
你要有上面三个配置
3、然后pom文件添加依赖
com.aliyun
aliyun-java-sdk-core
3.5.0
com.aliyun
aliyun-java-sdk-green
3.5.0
4、然后就简单了 copy 官方sdk的sample就可以了 简单改一改即可用
package com.hqjl.communityserv.imageCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Properties;
/**
* Created by liuhai.lh on 17/01/12.
*/
public class BaseRequest {
private static final Logger logger = LoggerFactory.getLogger(BaseRequest.class);
protected static String accessKeyId;
protected static String accessKeySecret;
protected static String regionId;
static {
Properties properties = new Properties();
try {
properties.load(BaseRequest.class.getResourceAsStream("/config.properties"));
accessKeyId = properties.getProperty("accessKeyId");
accessKeySecret = properties.getProperty("accessKeySecret");
regionId = properties.getProperty("regionId");
} catch(IOException e) {
logger.error(e.getMessage(), e);
}
}
protected static String getDomain(){
if("cn-shanghai".equals(regionId)){
return "green.cn-shanghai.aliyuncs.com";
}
if ("cn-beijing".equals(regionId)) {
return "green.cn-beijing.aliyuncs.com";
}
if ("ap-southeast-1".equals(regionId)) {
return "green.ap-southeast-1.aliyuncs.com";
}
if ("us-west-1".equals(regionId)) {
return "green.us-west-1.aliyuncs.com";
}
return "green.cn-shanghai.aliyuncs.com";
}
protected static String getEndPointName(){
return regionId;
}
}
package com.hqjl.communityserv.imageCheck;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* @author chunying.wang
*/
@Component
public class ImageSyncScanRequest extends BaseRequest {
private final Logger LOG = LoggerFactory.getLogger(ImageSyncScanRequest.class);
private final String AD = "ad";
private final String PASS = "pass";
public Boolean checkPic(String url) throws Exception{
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint(getEndPointName(), regionId, "Green", getDomain());
IAcsClient client = new DefaultAcsClient(profile);
com.aliyuncs.green.model.v20180509.ImageSyncScanRequest imageSyncScanRequest = new com.aliyuncs.green.model.v20180509.ImageSyncScanRequest();
imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageSyncScanRequest.setEncoding("utf-8");
imageSyncScanRequest.setRegionId(regionId);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task = new LinkedHashMap<String, Object>();
task.put("dataId", UUID.randomUUID().toString());
task.put("url", url);
task.put("time", new Date());
tasks.add(task);
JSONObject data = new JSONObject();
/**
* porn: 色情
* terrorism: 暴恐
* qrcode: 二维码
* ad: 图片广告
* ocr: 文字识别
*/
data.put("scenes", Arrays.asList(AD));
data.put("tasks", tasks);
imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageSyncScanRequest.setConnectTimeout(3000);
imageSyncScanRequest.setReadTimeout(10000);
try {
HttpResponse httpResponse = client.doAction(imageSyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
// System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if(200 == ((JSONObject)taskResult).getInteger("code")){
JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject)sceneResult).getString("scene");
String suggestion = ((JSONObject)sceneResult).getString("suggestion");
//根据scene和suggetion做相关的处理
if (!suggestion.equals(PASS)){
return false;
}
// System.out.println("args = [" + scene + "]");
// System.out.println("args = [" + suggestion + "]");
}
}else{
LOG.error("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
LOG.error("msg=" + scrResponse.getString("msg"));
return false;
}
}
} else {
LOG.error("detect not success. code:" + scrResponse.getInteger("code"));
LOG.error("msg=" + scrResponse.getString("msg"));
return false;
}
} else {
LOG.error("response not success. status:" + httpResponse.getStatus());
LOG.error(httpResponse.toString());
return false;
}
} catch (ServerException e) {
LOG.error(e.getMessage(), e);
return false;
} catch (ClientException e) {
LOG.error(e.getMessage(), e);
return false;
} catch (Exception e){
LOG.error(e.getMessage(), e);
return false;
}
return true;
}
}
简单的把url变成参数,把日志输出改成Logger 就ok了,
这里会有很多坑,传给他们的url需要是全路径,访问时间根据图片下载时间定,所以我用的是阿里云存图片。