图片音视频过滤有好多方法,我这里就不一一介绍了,这篇文章只是简单介绍一下我在项目中使用阿里云oss滤网过滤的步骤
1.所遇问题:
1.图片视频鉴别时要设置textScanRequest.setUriPattern(uri);否则会报msg=[scene] is not support
2.内容安全内容检测API服务接入地址为:http://green.cn-shanghai.aliyuncs.com(上海)或http://green.cn-hangzhou.aliyuncs.com(杭州)
3.通信协议:内容安全内容检测API服务仅支持使用HTTP协议发送请求
4.所有接口仅支持HTTP POST方法发送请求
2.所需jar包
aliyun-java-sdk-core-3.0.7.jar
aliyun-java-sdk-green-3.0.0.jaraliyun-sdk-oss-2.6.0-javadoc.jaraliyun-sdk-oss-2.6.0-sources.jaraliyun-sdk-oss-2.6.0.jar以上jar包请前往https://help.aliyun.com/knowledge_detail/50170.html下载
2.设置公共请求参数
参考文档:
域名目前只支持"cn-hangzhou"和"cn-hangzhou"
// 请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile(域名, accessKeyId,accessKeySecret);
// 内容安全内容检测API服务接入地址为:http://green.cn-hangzhou.aliyuncs.com
DefaultProfile.addEndpoint(需要检测的文件名, 域名, "Green", "green.cn-hangzhou.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
TextScanRequest textScanRequest = new TextScanRequest();
textScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
textScanRequest.setContentType(FormatType.JSON);
textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 所有接口仅支持HTTP
// POST方法发送请求,这种方式下请求参数需要包含在请求的URL中
textScanRequest.setEncoding("UTF-8");// 请求及返回结果都使用UTF-8字符集进行编码
textScanRequest.setRegionId(域名);
3.方法调用
参考文档:https://help.aliyun.com/document_detail/53423.html?spm=5176.doc28440.6.564.NWZQ2p
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", text); tasks.add(task1); JSONObject data = new JSONObject(); data.put("scenes", Arrays.asList("antispam")); data.put("tasks", tasks);textScanRequest.setContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
try {
HttpResponse httpResponse = client.doAction(textScanRequest);if (httpResponse.isSuccess()) {
JSONObject jo = JSON.parseObject(new String(httpResponse.getContent(), "UTF-8")); Gson gson = new Gson(); TextBean fromJson = gson.fromJson(jo.toString(), TextBean.class); List<TextBean.DataBean> data2 = fromJson.getData(); for (int i = 0; i < data2.size(); i++) { TextBean.DataBean dataBean = data2.get(i); List<TextBean.DataBean.ResultsBean> results = dataBean.getResults(); for (int j = 0; j < results.size();) { TextBean.DataBean.ResultsBean resultsBean = results.get(j); String suggestion = resultsBean.getSuggestion(); if (suggestion.equals("pass")) { return true; } else { return false; } } }} else {
return false; }} catch (ServerException e) {
e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return true; }