官方文档
阿里云oss违规检测,内容安全
SDK
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-green</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
还是直接上代码
package cn.country.cunyue.app.application.utils;
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.green.model.v20180509.*;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class ContentSecurityUtils {
public static void imageContentSecurity(String url) throws ClientException {
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
imageSyncScanRequest.setMethod(MethodType.POST);
imageSyncScanRequest.setEncoding("utf-8");
imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
JSONObject httpBody = new JSONObject();
httpBody.put("scenes", Arrays.asList("porn"));
JSONObject task = new JSONObject();
task.put("dataId", UUID.randomUUID().toString());
task.put("url", url);
task.put("time", new Date());
httpBody.put("tasks", Arrays.asList(task));
imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
imageSyncScanRequest.setConnectTimeout(3000);
imageSyncScanRequest.setReadTimeout(10000);
HttpResponse httpResponse = null;
try {
httpResponse = client.doAction(imageSyncScanRequest);
} catch (Exception e) {
e.printStackTrace();
}
if (httpResponse != null && httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
System.out.println(JSON.toJSONString(scrResponse, true));
int requestCode = scrResponse.getIntValue("code");
JSONArray taskResults = scrResponse.getJSONArray("data");
if (200 == requestCode) {
for (Object taskResult : taskResults) {
int taskCode = ((JSONObject) taskResult).getIntValue("code");
JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
if (200 == taskCode) {
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject) sceneResult).getString("scene");
String suggestion = ((JSONObject) sceneResult).getString("suggestion");
System.out.println("scene = [" + scene + "]");
System.out.println("suggestion = [" + suggestion + "]");
}
} else {
System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
}
}
} else {
System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
}
}
}
public static void videoContentSecurity(String url) throws Exception {
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
videoAsyncScanRequest.setAcceptFormat(FormatType.JSON);
videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
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);
tasks.add(task);
JSONObject data = new JSONObject();
data.put("scenes", Arrays.asList("porn", "terrorism"));
data.put("tasks", tasks);
data.put("callback", "http://xxx.xxx.xx/xxx.json");
data.put("seed", "yourPersonalSeed");
videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
videoAsyncScanRequest.setConnectTimeout(3000);
videoAsyncScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(jsonObject, true));
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
public static void textContentSecurity(String textContent) throws Exception {
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您的AccessKeyId", "您的AccessKeySecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
TextScanRequest textScanRequest = new TextScanRequest();
textScanRequest.setAcceptFormat(FormatType.JSON);
textScanRequest.setHttpContentType(FormatType.JSON);
textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
textScanRequest.setEncoding("UTF-8");
textScanRequest.setRegionId("cn-shanghai");
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("dataId", UUID.randomUUID().toString());
task1.put("content", textContent);
tasks.add(task1);
JSONObject data = new JSONObject();
data.put("scenes", Arrays.asList("antispam"));
data.put("tasks", tasks);
System.out.println(JSON.toJSONString(data, true));
textScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
textScanRequest.setConnectTimeout(3000);
textScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(textScanRequest);
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");
System.out.println("args = [" + scene + "]");
System.out.println("args = [" + suggestion + "]");
}
} else {
System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void voiceContentSecurity(String url) throws Exception {
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您自己的AccessKey ID", "您自己的AccessKey Secret");
final IAcsClient client = new DefaultAcsClient(profile);
VoiceSyncScanRequest asyncScanRequest = new VoiceSyncScanRequest();
asyncScanRequest.setAcceptFormat(FormatType.JSON);
asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
asyncScanRequest.setRegionId("cn-shanghai");
asyncScanRequest.setConnectTimeout(3000);
asyncScanRequest.setReadTimeout(15000);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("url", "https://xxxxxxxxxxxxxx");
tasks.add(task1);
JSONObject data = new JSONObject();
System.out.println("==========Task count:" + tasks.size());
data.put("scenes", Arrays.asList("antispam"));
data.put("tasks", tasks);
asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
System.out.println(JSON.toJSONString(data, true));
try {
HttpResponse httpResponse = client.doAction(asyncScanRequest);
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) {
Integer code = ((JSONObject) taskResult).getInteger("code");
if (200 == code) {
System.out.println("task process success, result:" + JSON.toJSONString(taskResult));
} else {
System.out.println("task process fail: " + JSON.toJSONString(taskResult));
}
}
} else {
System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
}
} else {
System.out.println("response fail:" + new String(httpResponse.getHttpContent(), "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}