P15GEN2\59518
2025-10-18 56638c01bb2cc61a92f5e03c9a1001be5b5d3699
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
        }
    }
}