File moderation
This topic describes how to use the Java software development kit (SDK) file moderation API to moderate text and images in files.
Feature description
File moderation is an asynchronous process and does not return moderation results in real time. For more information about the parameters, see Asynchronous file moderation.
Feature | File detection |
Detects the image file format. | DOC, DOCX, PPT, PPTX, PDF, XLS, XLSX, and TXT. |
Text detection in images | Support |
File size limit | 200 MB |
Number of concurrent file detection tasks | 10 |
Offline moderation mode (for batch file moderation) | Supported |
Other |
|
Prerequisites
Java dependencies are installed. For more information, see Installation.
NoteYou must use the Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.
Submit an asynchronous file moderation task
API | Description | Supported regions |
FileAsyncScanV2Request | Submits an asynchronous task to moderate text and images in a file. |
|
Sample code
package demo;
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.FileAsyncScanV2Request;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.commons.codec.binary.StringUtils;
import java.util.Arrays;
public class FileAsyncScanV2Demo {
public static void main(String[] args) throws Exception {
/**
* An Alibaba Cloud account AccessKey has full permissions for all APIs. For better security, use a Resource Access Management (RAM) user for API calls and daily O&M.
* Common ways to obtain environment variables:
* Method 1:
* Get the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Get the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"Obtain the RAM user AccessKey ID from an environment variable",
"Obtain the RAM user AccessKey secret from an environment variable");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
// Note: Reuse the instantiated client to improve moderation performance. This avoids creating repeated connections.
IAcsClient client = new DefaultAcsClient(profile);
FileAsyncScanV2Request fileAsyncScanV2Request = new FileAsyncScanV2Request();
fileAsyncScanV2Request.setAcceptFormat(FormatType.JSON);
fileAsyncScanV2Request.setMethod(com.aliyuncs.http.MethodType.POST);
fileAsyncScanV2Request.setEncoding("UTF-8");
JSONObject data = new JSONObject();
data.put("modType", "All");
data.put("maxPages", 200);
data.put("offline", true);
data.put("bizType", "aligreen_default_bizType");
JSONObject task = new JSONObject();
task.put("url", "The URL of the file to moderate");
data.put("tasks", Arrays.asList(task));
fileAsyncScanV2Request.setHttpContent(StringUtils.getBytesUtf8(data.toJSONString()), "UTF-8", FormatType.JSON);
fileAsyncScanV2Request.setConnectTimeout(3000);
fileAsyncScanV2Request.setReadTimeout(10000);
HttpResponse httpResponse = null;
try {
httpResponse = client.doAction(fileAsyncScanV2Request);
if (httpResponse.isSuccess()) {
JSONObject responseObj = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
int responseCode = responseObj.getIntValue("code");
// The moderation result of each file.
JSONArray taskResults = responseObj.getJSONArray("data");
if (200 == responseCode) {
for (Object taskResult : taskResults) {
// The processing result of a single file.
int taskCode = ((JSONObject) taskResult).getIntValue("code");
if (200 == taskCode) {
// Save the taskId to poll the result.
System.out.println(((JSONObject) taskResult).getString("taskId"));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}Query asynchronous file moderation results
API | Description | Supported regions |
FileAsyncScanResultsRequest | Queries the result of an asynchronous file moderation task. |
|
Sample code
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.FileAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
/**
* An Alibaba Cloud account AccessKey has full permissions for all APIs. For better security, use a Resource Access Management (RAM) user for API calls and daily O&M.
* Common ways to obtain environment variables:
* Method 1:
* Get the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Get the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"Obtain the RAM user AccessKey ID from an environment variable",
"Obtain the RAM user AccessKey secret from an environment variable");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
// Note: Reuse the instantiated client to improve moderation performance. This avoids creating repeated connections.
IAcsClient client = new DefaultAcsClient(profile);
FileAsyncScanResultsRequest fileAsyncScanResultsRequest = new FileAsyncScanResultsRequest();
// Specify the API response format.
fileAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);
// Specify the request method.
fileAsyncScanResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST);
fileAsyncScanResultsRequest.setEncoding("utf-8");
JSONObject data = new JSONObject();
data.put("taskIds", Arrays.asList("The ID of the file moderation task"));
fileAsyncScanResultsRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(data.toJSONString()),
"UTF-8", FormatType.JSON);
/**
* Set the timeout period. The server-side timeout period for a full-link process is 10 seconds. Set the timeout period accordingly.
* If the ReadTimeout value is less than the server-side processing time, a ReadTimeout exception occurs in your program.
*/
fileAsyncScanResultsRequest.setConnectTimeout(3000);
fileAsyncScanResultsRequest.setReadTimeout(10000);
HttpResponse httpResponse = null;
try {
httpResponse = client.doAction(fileAsyncScanResultsRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
int requestCode = scrResponse.getIntValue("code");
// The moderation result of each file.
JSONArray taskResults = scrResponse.getJSONArray("data");
if (200 == requestCode) {
for (Object taskResult : taskResults) {
// The processing result of a single file.
int taskCode = ((JSONObject) taskResult).getIntValue("code");
if (200 == taskCode) {
// The task ID.
System.out.println(((JSONObject) taskResult).getString("taskId"));
// The text moderation result if textScenes is specified.
JSONArray textResults = ((JSONObject) taskResult).getJSONArray("textResults");
// The image moderation result if imageScenes is specified.
JSONArray imageResults = ((JSONObject) taskResult).getJSONArray("imageResults");
} else {
// The file failed to be processed. Analyze the cause as needed.
System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
}
}
} else {
/**
* The entire request failed. Analyze the cause as needed.
*/
System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}