fei
2025-09-09 19f5c982ea9ad8d7aad65b2c9a776b829992d2ca
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.ruoyi.service.impl;
 
import com.ruoyi.service.IArchiveDoublePdfGenerateService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
 
 
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;
 
@Service
public class IArchiveDoublePdfGenerateServiceImpl implements IArchiveDoublePdfGenerateService {
    private final  String OCRServer = "http://127.0.0.1:1224/";
    private  String MUL_LAYER_PDF_URL = OCRServer + "api/doc/upload";
    private  String MUL_LAYER_PDF_STATE_URL = OCRServer + "api/doc/result";
    private  String MUL_LAYER_PDF_DOWNLOAD_URL = OCRServer + "api/doc/download";
 
 
 
 
    public void testConnection()
    {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(OCRServer);
 
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
    }
 
    public void doublePdfGenerate() throws IOException {
        // 构建请求URL和文件路径
        String url = "http://127.0.0.1:1224/api/doc/upload";
        String path = "测试文件.pdf";
        String options_json = "{\"doc.extractionMode\": \"fullPage\"}";
 
// 创建HttpClient实例
//        HttpClient client = HttpClient.newHttpClient();
 
//// 生成boundary用于分隔表单数据
//        String boundary = UUID.randomUUID().toString();
//
//// 构建multipart/form-data请求体
//        StringBuilder sb = new StringBuilder();
//// 添加JSON参数部分
//        sb.append("--").append(boundary).append("\r\n");
//        sb.append("Content-Disposition: form-data; name=\"json\"\r\n");
//        sb.append("Content-Type: application/json\r\n");
//        sb.append("\r\n");
//        sb.append(options_json).append("\r\n");
//
//// 添加文件部分
//        sb.append("--").append(boundary).append("\r\n");
//        sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"")
//                .append(Paths.get(path).getFileName()).append("\"\r\n");
//        sb.append("Content-Type: application/pdf\r\n"); // 明确指定PDF类型
//        sb.append("\r\n");
//
//// 读取文件内容并构建完整请求体
//        byte[] fileBytes = Files.readAllBytes(Paths.get(path));
//        byte[] requestBody = new byte[sb.toString().getBytes().length + fileBytes.length
//                + ("\r\n--" + boundary + "--\r\n").getBytes().length];
//        System.arraycopy(sb.toString().getBytes(), 0, requestBody, 0, sb.toString().getBytes().length);
//        System.arraycopy(fileBytes, 0, requestBody, sb.toString().getBytes().length, fileBytes.length);
//        System.arraycopy(("\r\n--" + boundary + "--\r\n").getBytes(), 0, requestBody,
//                sb.toString().getBytes().length + fileBytes.length,
//                ("\r\n--" + boundary + "--\r\n").getBytes().length);
//
//// 创建并发送请求
//        HttpRequest request = HttpRequest.newBuilder()
//                .uri(URI.create(url))
//                .header("Content-Type", "multipart/form-data; boundary=" + boundary)
//                .POST(HttpRequest.BodyPublishers.ofByteArray(requestBody))
//                .build();
//
//// 处理响应
//        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//        System.out.println("响应状态码: " + response.statusCode());
//        System.out.println("响应内容: " + response.body());
    }
}