Commit ead3fd2d authored by 刘基明's avatar 刘基明

图片审核test

parent bace6b8d
......@@ -189,6 +189,28 @@
<version>4.0.11</version>
</dependency>
<!-- 阿里云内容检测-->
<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.5</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>
</dependencies>
<build>
......
package com.tanpu.community.controller;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.req.OSSCheckReq;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping(value = "/api")
public class CallBackController {
@ApiOperation("发表评论")
@ApiOperation("oss图片审核回调")
@PostMapping(value = "/picCheck/callback")
@ResponseBody
public CommonResp<Void> picCheck(@RequestBody OSSCheckReq req) {
log.warn(JsonUtil.toJson(req));
return CommonResp.success();
public String picCheck(String checksum,String content) {
Map<String, String> params = new HashMap<>();
log.info("params:{}", checksum);
log.info("content:{}", content);
return "success";
}
......
package com.tanpu.community.service;
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.ImageAsyncScanResultsRequest;
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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.AliyunOSSHelper;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
......@@ -22,6 +35,7 @@ import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -119,4 +133,65 @@ public class OSSFileService {
return fileRecordMapper.selectList(new LambdaQueryWrapper<FileRecordEntity>()
.in(FileRecordEntity::getFileId,fileIds));
}
public static void main(String[] args) 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);
ImageAsyncScanResultsRequest imageAsyncScanResultsRequest = new ImageAsyncScanResultsRequest();
// 指定API返回格式。
imageAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);
// 指定请求方法。
imageAsyncScanResultsRequest.setMethod(MethodType.POST);
imageAsyncScanResultsRequest.setEncoding("utf-8");
// 支持HTTP和HTTPS。
imageAsyncScanResultsRequest.setProtocol(ProtocolType.HTTP);
List<String> taskIds = new ArrayList<String>();
taskIds.add("img4hDosCHcrFk5jAMR80XWJN-1pZ@0p");
imageAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间。
*/
imageAsyncScanResultsRequest.setConnectTimeout(3000);
imageAsyncScanResultsRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncScanResultsRequest);
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和suggestion做相关的处理。
// 根据不同的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();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment