本文介绍如何使用Java SDK网页审核接口检测网页中的文字和图片信息,并同步返回检测出来的结果。
准备工作
在进行具体的服务调用之前,请完成以下准备工作:
- 创建阿里云AccessKey。关于创建阿里云AccessKey的具体操作,请参见创建AccessKey。
- 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖。
- 可选:如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
提交网页同步检测任务
接口 | 描述 | 支持的地域 |
---|---|---|
WebpageSyncScanRequest | 提交网页同步检测任务,对网页中的文字和图片进行检测。 |
|
示例代码
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.green.model.v20180509.WebpageSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Arrays;
import java.util.UUID;
/**
* 通过如下代码进行网页同步检测。
*/
public class WebPageSyncScanSample {
private static final String aliyunAccessKeyId = "您的AccessKey Id";
private static final String aliyunAccessKeySecret = "您的AccessKey Secret";
private static String regionId = "cn-shanghai";
private static String endpoint = "green.cn-shanghai.aliyuncs.com";
public static void main(String[] args) {
IClientProfile profile = DefaultProfile.getProfile(regionId, aliyunAccessKeyId, aliyunAccessKeySecret);
DefaultProfile.addEndpoint(regionId, "Green", endpoint);
IAcsClient client = new DefaultAcsClient(profile);
WebpageSyncScanRequest request = new WebpageSyncScanRequest();
// 请设置连接超时时间,您可根据实际情况进行修改。单位:ms。
request.setSysConnectTimeout(3000);
// 请设置超时时间,您可根据实际情况进行修改。单位:ms。
request.setSysReadTimeout(9000);
// 构建请求消息。
JSONObject requestContent = new JSONObject();
requestContent.put("bizType", "webPage");
requestContent.put("textScenes", Arrays.asList("antispam"));
requestContent.put("imageScenes", Arrays.asList("porn", "terrorism"));
JSONObject task = new JSONObject();
task.put("dataId", UUID.randomUUID().toString());
task.put("url", "https://www.xxxx.com"); // 待检测的网页URL。
requestContent.put("tasks", Arrays.asList(task));
try {
request.setHttpContent(requestContent.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
HttpResponse httpResponse = client.doAction(request);
String responseBody = httpResponse.getHttpContentString();
// HTTP请求成功。
if (httpResponse.isSuccess()) {
JSONObject responseData = JSON.parseObject(responseBody);
int respCode = responseData.getIntValue("code");
// 结果处理。
if (respCode == 200) {
JSONArray taskResults = responseData.getJSONArray("data");
for (int i = 0; i < taskResults.size(); i++) {
JSONObject taskRst = taskResults.getJSONObject(i);
int taskCode = taskRst.getIntValue("code");
if (taskCode == 200) {
// 任务处理成功。
System.out.println("task success : " + JSON.toJSONString(taskRst));
// 根据taskRst中返回的检测结果进行业务处理。
} else {
// 单个任务失败,分析失败原因。
System.out.println("task fail : " + JSON.toJSONString(taskRst));
}
}
} else {
// 整个结果失败,需要分析失败原因。
System.out.println("response fail : " + responseBody);
}
} else {
// 请求失败,需要分析分析错误内容。
System.out.println("request fail : " + responseBody);
}
} catch (Exception e) {
// 请求发生异常的处理。
e.printStackTrace();
}
}
}
提交网页异步检测任务
接口 | 描述 | 支持的Region |
---|---|---|
WebpageAsyncScanRequest | 提交网页异步检测任务,对网页中的文字和图片进行检测。 |
|
示例代码
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.green.model.v20180509.WebpageAsyncScanRequest;
import com.aliyuncs.green.model.v20180509.WebpageAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
/**
* 通过如下代码进行网页异步检测。
*/
public class WebAsyncScanSample {
private static final String aliyunAccessKeyId = "您的AccessKey Id";
private static final String aliyunAccessKeySecret = "您的AccessKey Secret";
private static String regionId = "cn-shanghai";
private static String endpoint = "green.cn-shanghai.aliyuncs.com";
public static void main(String[] args) throws InterruptedException {
IClientProfile profile = DefaultProfile.getProfile(regionId, aliyunAccessKeyId, aliyunAccessKeySecret);
DefaultProfile.addEndpoint(regionId, "Green", endpoint);
IAcsClient client = new DefaultAcsClient(profile);
// 提交网页异步检测任务。
String taskId = submitAsyncTask(client);
if (taskId != null) {
// 轮询任务结果。
for (int i = 0; i < 100; i++) {
JSONObject ret = queryResult(client, taskId);
if (ret == null) {
Thread.sleep(5000);
} else {
break;
}
}
}
}
private static String submitAsyncTask(IAcsClient client) {
WebpageAsyncScanRequest request = new WebpageAsyncScanRequest();
request.setSysConnectTimeout(3000);
request.setSysReadTimeout(9000);
// 构建请求消息。
JSONObject requestContent = new JSONObject();
requestContent.put("bizType", "webPage");
requestContent.put("textScenes", Arrays.asList("antispam"));
requestContent.put("imageScenes", Arrays.asList("porn"));
// 通过回调接收结果。
// requestContent.put("callback", "http://xxx.com/result/recv");
// requestContent.put("seed", "seedxxxxxxx");
JSONObject task = new JSONObject();
task.put("dataId", UUID.randomUUID().toString());
// 待检测的网页URL。
task.put("url", "https://xxx.com/index.html");
requestContent.put("tasks", Arrays.asList(task));
try {
request.setHttpContent(requestContent.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
HttpResponse httpResponse = client.doAction(request);
String responseBody = httpResponse.getHttpContentString();
if (!httpResponse.isSuccess()) {
// 请求失败,需要分析错误内容。
System.out.println("request fail : " + responseBody);
} else {
JSONObject responseData = JSON.parseObject(responseBody);
int respCode = responseData.getIntValue("code");
if (respCode != 200) {
System.out.println("response fail : " + responseBody);
} else {
JSONArray taskResults = responseData.getJSONArray("data");
JSONObject taskRst = taskResults.getJSONObject(0);
int taskCode = taskRst.getIntValue("code");
if (taskCode != 200) {
System.out.println("task fail : " + JSON.toJSONString(taskRst));
} else {
System.out.println("task success : " + JSON.toJSONString(taskRst));
return taskRst.getString("taskId");
}
}
}
} catch (Exception e) {
// 请求发生异常的处理。
e.printStackTrace();
}
return null;
}
private static JSONObject queryResult(IAcsClient client, String taskId) {
WebpageAsyncScanResultsRequest request = new WebpageAsyncScanResultsRequest();
// 请设置连接超时时间,您可根据实际情况进行修改。单位:ms。
request.setSysConnectTimeout(3000);
// 请设置超时时间,您可根据实际情况进行修改。单位:ms。
request.setSysReadTimeout(9000);
// 构建请求消息。
List<String> taskIds = Arrays.asList(taskId);
try {
request.setHttpContent(JSONObject.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
HttpResponse httpResponse = client.doAction(request);
String responseBody = httpResponse.getHttpContentString();
JSONObject responseData = JSON.parseObject(responseBody);
if (!httpResponse.isSuccess()) {
// 请求失败,返回对应的错误码。
System.out.println("request fail : " + responseBody);
return responseData;
} else {
int respCode = responseData.getIntValue("code");
if (respCode != 200) {
System.out.println("response fail : " + responseBody);
return responseData;
} else {
JSONArray taskResults = responseData.getJSONArray("data");
JSONObject taskRst = taskResults.getJSONObject(0);
int taskCode = taskRst.getIntValue("code");
if (taskCode == 280) {
System.out.println("task processing : " + JSON.toJSONString(taskRst));
return null;
} else if (taskCode != 200) {
System.out.println("task fail : " + JSON.toJSONString(taskRst));
return taskRst;
} else {
System.out.println("task success : " + JSON.toJSONString(taskRst));
return taskRst;
}
}
}
} catch (Exception e) {
// 请求发生异常的处理。
e.printStackTrace();
}
return null;
}
}