1.引入maven依赖
2.
package com.ck.common.aip;
import com.alibaba.fastjson.JSONObject;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.ck.common.utils.ToolUtils;
import com.ck.core.JsonResult;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class VcrUtil {
//获取accessToken
// 官网获取的 API Key 更新为你注册的
private static String clientId="G61aeZSjfdkjliwssUbjd0o";
//官网获取的 Secret Key 更新为你注册的
private static String clientSecret="gH5FhFjFUNJFHJAQrGO6nPEWjw";
//获取accesToken地址
private static String oauthPath="https://aip.baidubce.com/oauth/2.0/token";
//图片审核api地址
private static String imgPath="https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
//文本审核api地址
private static String textPath="https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
//视频审核api地址
private static String videoPath="https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v2/user_defined";
//鉴权认证机制 获取AccessToken,因为AccessToken的有效期为30天,所以我将它存储到redis,当redis里的Accesstoken失效了,重新获取。
private static String getAuth() {
ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, oauthPath);
// 设置header参数
request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
// 设置query参数
request.addQueryParameter("client_id", clientId);
request.addQueryParameter("client_secret", clientSecret);
request.addQueryParameter("grant_type", "client_credentials");
ApiExplorerClient client = new ApiExplorerClient();
try {
ApiExplorerResponse response = client.sendRequest(request);
// 返回结果格式为Json字符串
System.out.println(response.getResult());
String result = response.getResult();
if (StringUtils.isNotBlank(result)) {
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.containsKey("access_token")) {
return jsonObject.getString("access_token");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
//从redis中获取AccessToken,如果没有,调用getAuth获取,然后同步redis的AccessToken
private static String getAccessToken(){
String accessToken = ToolUtils.getAccessToken();
if (StringUtils.isNotBlank(accessToken)){
return accessToken;
}else{
ToolUtils.delAccessToken();
String auth = getAuth();
ToolUtils.setAccessToken(auth);
return auth;
}
}
//图片审核方法
public static Boolean checkImage(String imgUrl){
try {
String jsonBody = "imgUrl="+ URLEncoder.encode(imgUrl,"UTF-8")+"&imgType=0";
return check(jsonBody,imgPath);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
//文本审核方法
public static Boolean checkText(String text){
try {
String jsonBody = "text="+ URLEncoder.encode(text,"UTF-8");
return check(jsonBody,textPath);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
public static Boolean checkVideo(String video,String name){
try {
String jsonBody = "videoUrl="+video+"&extId="+video+"&name="+name;
return check(jsonBody,videoPath);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
//审核
public static Boolean check(String jsonBody,String path) {
ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
// 设置header参数
request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
// 设置query参数
request.addQueryParameter("access_token", getAccessToken());
// 设置jsonBody参数
try {
request.setJsonBody(jsonBody);
ApiExplorerClient client = new ApiExplorerClient();
ApiExplorerResponse response = client.sendRequest(request);
// 返回结果格式为Json字符串
System.out.println(response.getResult());
return response(response.getResult());
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
//审核结果返回,我这里是返回boolean,如果有需要详情,自行更改
private static Boolean response(String data){
Boolean flag=false;
JSONObject jsonObject = JSONObject.parseObject(data);
if (jsonObject.containsKey("conclusionType")){
int conclusionType=jsonObject.getIntValue("conclusionType");
if (conclusionType==1){
flag=true;
}
}
return flag;
}
}
package com.ck.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
*
*/
@Component
public class ToolUtils {
//RedisUtil 对redis使用的工具类,如果没有,网上一大把。
@Autowired
protected RedisUtil redisUtil;
private static ToolUtils toolUtils;
@PostConstruct
public void init(){
toolUtils=this;
toolUtils.redisUtil=this.redisUtil;
}
//获取AccessToken
public static String getAccessToken(){
//存储redis的有效期为25天
Object accessToken = toolUtils.redisUtil.get("AccessToken");
if (accessToken!=null) {
return accessToken.toString();
}
return "";
}
public static void setAccessToken(String accessToken){
toolUtils.redisUtil.set("AccessToken",accessToken,25*24*60*60);
}
//删除AccessToken
public static void delAccessToken(){
toolUtils.redisUtil.del("AccessToken");
}
}