package ai;
|
|
import java.nio.charset.StandardCharsets;
|
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
import foundation.dao.DataPackage;
|
import foundation.data.entity.Entity;
|
import foundation.icall.ICall;
|
import foundation.icall.ICallBucket;
|
import foundation.icall.ICallCenter;
|
import foundation.util.Util;
|
import foundation.workflow.ActionProvider;
|
|
public class AiHandler extends ActionProvider {
|
|
@Override
|
protected void publishMethod() {
|
addMethod("pdf");
|
|
addMethod("writerResult");
|
}
|
|
public void pdf() throws Exception {
|
AliyunOcrApiDirect ocr = new AliyunOcrApiDirect();
|
|
DataPackage dataPackage = dataReader.getDataPackage();
|
dataPackage.loadOneDataFromDB();
|
Entity master = dataPackage.getMasterEntity();
|
String fileUrl = master.getString("file_url");
|
// PDF识别
|
String result = ocr.recognizePdf(fileUrl, 1);
|
System.out.println("PDF识别结果: " + result);
|
}
|
|
public void writerResult() throws Exception {
|
DataPackage dataPackage = dataReader.getDataPackage();
|
dataPackage.loadOneDataFromDB();
|
Entity master = dataPackage.getMasterEntity();
|
|
int index = 0;
|
|
String baiduFileUrl = master.getString("baidu_file_url");
|
ICallCenter icallCenter = ICallCenter.getInstance();
|
ICallBucket callBucket = ICallBucket.getInstance();
|
ICall iCall = callBucket.getOne("document-parser-quary");
|
|
while (Util.isEmpty(baiduFileUrl) && index < 3) {
|
step.setDataPackage(dataPackage);
|
icallCenter.callRemote(step, iCall);
|
|
dataPackage.loadOneDataFromDB(true);
|
master = dataPackage.getMasterEntity();
|
index ++;
|
|
Thread.sleep(2000);
|
baiduFileUrl = master.getString("baidu_file_url");
|
}
|
|
String jsonContent = fetchJsonWithHttpClient(baiduFileUrl);
|
AIResult result = new AIResult(jsonContent);
|
dataWriter.addValue("content", result);
|
dataWriter.addValue("data", master);
|
}
|
|
public static String fetchJsonWithHttpClient(String url) {
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
HttpGet request = new HttpGet(url);
|
|
// 设置请求头
|
request.setHeader("Accept", "application/json; charset=UTF-8");
|
request.setHeader("Accept-Charset", "UTF-8");
|
request.setHeader("User-Agent", "Mozilla/5.0");
|
|
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
int statusCode = response.getCode();
|
|
if (statusCode == 200) {
|
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
} else {
|
System.err.println("请求失败,状态码: " + statusCode);
|
return null;
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
}
|