|
|
@@ -4,8 +4,6 @@ import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.sm.AiBaiduCheckResult; |
|
|
|
import okhttp3.*; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
@@ -14,6 +12,8 @@ import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLConnection; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
/** |
|
|
|
* 图片质量检查方法 |
|
|
@@ -29,9 +29,7 @@ public class BaiduImageCheckUtil { |
|
|
|
// 百度Secret Key |
|
|
|
private final static String secretKey = "RY5YCqGejrc32xT5kIxDj0mqP9QRiKfD"; |
|
|
|
//写死的 access_token |
|
|
|
private final static String access_token = "24.c1c7992795301773c60c8cf01abfe759.2592000.1689236723.282335-33618414"; |
|
|
|
|
|
|
|
public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build(); |
|
|
|
private final static String access_token = "24.ba201483ba860779dcb402cc044a3bd4.2592000.1691298224.282335-35825024"; //7.7 |
|
|
|
|
|
|
|
public static ResultData photoCheck(MultipartFile file) { |
|
|
|
try { |
|
|
@@ -41,9 +39,10 @@ public class BaiduImageCheckUtil { |
|
|
|
String param = "image=" + imageUrl; |
|
|
|
// 注意这里access_token有过期时间, 客户端可自行缓存,过期后重新获取。 |
|
|
|
// Access Token的有效期(秒为单位,有效期30天) |
|
|
|
// String accessToken = getAuth(); |
|
|
|
String accessToken = getAuth(); |
|
|
|
|
|
|
|
//这里暂时写死 |
|
|
|
String result = BaiDuHttpUtil.post(photo_check_url, access_token, param); |
|
|
|
String result = BaiDuHttpUtil.post(photo_check_url, accessToken, param); |
|
|
|
System.out.println(result); |
|
|
|
JSONObject jsonObject = JSON.parseObject(result); |
|
|
|
//1:合规,2:不合规,3:疑似,4:审核失败 |
|
|
@@ -99,21 +98,37 @@ public class BaiduImageCheckUtil { |
|
|
|
* 获取API访问权限token,该token有一定的有效期,需要自行管理,当失效时需重新获取。 |
|
|
|
* Access Token的有效期(秒为单位,有效期30天) |
|
|
|
*/ |
|
|
|
public static String getAuth() throws IOException { |
|
|
|
MediaType mediaType = MediaType.parse("application/json"); |
|
|
|
RequestBody body = RequestBody.create(mediaType, ""); |
|
|
|
Request request = new Request.Builder() |
|
|
|
.url(auth_url + "client_id=" + apiKey + "&client_secret= " + secretKey + "&grant_type=client_credentials") |
|
|
|
.method("POST", body) |
|
|
|
.addHeader("Content-Type", "application/json") |
|
|
|
.addHeader("Accept", "application/json") |
|
|
|
.build(); |
|
|
|
Response response = HTTP_CLIENT.newCall(request).execute(); |
|
|
|
System.out.println(response.body().string()); |
|
|
|
return response.body().string(); |
|
|
|
public static String getAuth() { |
|
|
|
HashMap<String, String> map = new HashMap<>(); |
|
|
|
map.put("client_id", apiKey); |
|
|
|
map.put("client_secret", secretKey); |
|
|
|
map.put("grant_type", "client_credentials"); |
|
|
|
String doPost = HttpUtil.doPost(auth_url, map); |
|
|
|
JSONObject json = JSONObject.parseObject(doPost); |
|
|
|
String access_token = (String) json.get("access_token"); |
|
|
|
Integer expires_in = (Integer) json.get("expires_in"); |
|
|
|
String checkExpires = checkExpires(expires_in); |
|
|
|
if (checkExpires == null){ |
|
|
|
return access_token; |
|
|
|
} |
|
|
|
return doPost; |
|
|
|
} |
|
|
|
|
|
|
|
private static String checkExpires(Integer expiresIn) { |
|
|
|
int days = expiresIn / 60 / 60 / 24; |
|
|
|
Date timeAfterDays = DateUtils.getTimeAfterDays(days, new Date()); |
|
|
|
if (timeAfterDays.before(new Date())){ |
|
|
|
String doPost = getAuth(); |
|
|
|
JSONObject json = JSONObject.parseObject(doPost); |
|
|
|
String access_token = (String) json.get("access_token"); |
|
|
|
Integer expires_in = (Integer) json.get("expires_in"); |
|
|
|
return access_token; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException { |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
System.out.println(getAuth()); |
|
|
|
} |
|
|
|
} |