阿里云的文本检测demo在本地调用没有问题,上传服务器后就一直卡在new DefaultAcsClient(profile)这里,也不抛异常也不报超时是什么原因啊
``` package com.space.wechat.util;
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.green.model.v20180509.TextScanRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException; import java.util.*;
public class AliTextScanUtil {
private static Logger logger = LoggerFactory.getLogger(AliTextScanUtil.class);
public static final String REGIONID = "cn-shanghai";
public static final String SCENCE = "antispam";
private static DefaultAcsClient defaultAcsClient;
private static AliTextScanUtil aliTextScanUtil;
public static void getInstance() {
if (aliTextScanUtil == null) {
aliTextScanUtil = new AliTextScanUtil();
init();
} else {
if (defaultAcsClient == null) {
init();
}
}
}
public static void init() {
logger.info("AliTextScanUtil-init;AK=" + OSSClientUtil.ALI_AK + " SK=" + OSSClientUtil.ALI_SK);
try {
setIAcsClient();
} catch (Exception e) {
logger.error("AliTextScanUtil-init出错,错误原因:" + e.getMessage(), e);
}
}
public static void setIAcsClient() {
DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", OSSClientUtil.ALI_AK, OSSClientUtil.ALI_SK);
logger.info("---------------开始获取client-------------------");
try {
defaultAcsClient = new DefaultAcsClient(profile);
}catch (Exception e) {
e.printStackTrace();
}
logger.info("---------------获取client成功-------------------");
}
public static Boolean textScan(String context, Boolean isTagContext) {
getInstance();
TextScanRequest textScanRequest = new TextScanRequest();
textScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
textScanRequest.setHttpContentType(FormatType.JSON);
textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
textScanRequest.setEncoding("UTF-8");
//地域设置
textScanRequest.setRegionId(REGIONID);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task = new LinkedHashMap<String, Object>();
List<String> contexts = new ArrayList<>();
logger.info("------------------------开始校验-----------------------");
//移除空白字符后判断是否为空白
if (StringUtil.isNullOrEmpty(StringUtil.replaceBlank(context))) {
return true;
}
//判断是否为标签文本
if (isTagContext) {
context = StringUtil.removeHtmlTagFromText(context);
}
//文本长度不超过10000个字符。
if (context.length()>10000) {
contexts = getScanContext(context);
} else {
contexts = Arrays.asList(context);
}
//设置检测任务
for (String scantext : contexts) {
task = new LinkedHashMap<String, Object>();
task.put("dataId", UUID.randomUUID().toString());
task.put("content", scantext);
tasks.add(task);
}
/**
* 检测场景。文本垃圾检测请传递antispam。
**/
List<String> scenes = new ArrayList<>();
scenes.add(SCENCE);
JSONObject params = new JSONObject();
params.put("scenes", scenes);
params.put("tasks", tasks);
try {
textScanRequest.setHttpContent(params.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 请务必设置超时时间。
textScanRequest.setConnectTimeout(3000);
textScanRequest.setReadTimeout(6000);
JSONObject scrResponse = new JSONObject();
logger.info("------------发送请求---------------------");
try {
HttpResponse httpResponse = defaultAcsClient.doAction(textScanRequest);
if(!httpResponse.isSuccess()){
logger.error("请求失败. status:" + httpResponse.getStatus());
return true;
}
scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
if (200 != scrResponse.getInteger("code")) {
logger.error("文本内容检测失败. code:" + scrResponse.getInteger("code"));
return true;
}
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
JSONObject resultJson = (JSONObject)taskResult;
if(200 != resultJson.getInteger("code")){
continue;
}
JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String suggestion = ((JSONObject) sceneResult).getString("suggestion");
if (!"pass".equals(suggestion)) {
return false;
}
}
}
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch ( ClientException e){
e.printStackTrace();
}
return true;
} public static List getScanContext(String context) { List contexts = new ArrayList<>(); for (int i = 0; i <= context.length()/10000; i++) { int start = i*10000; int end = (i+1)*10000; if (end >= context.length()) { end = context.length(); } String substring = context.substring(start, end); if (!StringUtil.isNullOrEmpty(substring)){ contexts.add(substring); } } return contexts; }
public static void main(String[] args) {
Boolean qqq = textScan("qqq", false);
}
}
```日志执行到开始获取client后就什么都没了
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。