package com.ruoyi.service.impl;
|
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.service.IArchiveDoublePdfGenerateService;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import org.apache.commons.io.IOUtils;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.*;
|
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 unicodeEscapeToChinese (String unicodeEscapedString) {
|
// 解码Unicode转义序列
|
StringBuilder decodedString = new StringBuilder();
|
int i = 0;
|
while (i < unicodeEscapedString.length()) {
|
if (unicodeEscapedString.startsWith("\\u", i)) {
|
// 找到一个Unicode转义序列
|
int codePoint = Integer.parseInt(unicodeEscapedString.substring(i + 2, i + 6), 16);
|
decodedString.appendCodePoint(codePoint);
|
i += 6; // 跳过这个Unicode转义序列
|
} else {
|
// 不是Unicode转义序列,直接添加字符
|
decodedString.append(unicodeEscapedString.charAt(i));
|
i++;
|
}
|
}
|
// 打印解码后的字符串
|
System.out.println(decodedString.toString());
|
}
|
|
|
public AjaxResult testConnection(MultipartFile multipartFile) throws IOException {
|
|
|
// 创建临时文件
|
File file = File.createTempFile("temp", ".pdf");
|
// 将 MultipartFile 转存到目标文件
|
multipartFile.transferTo(file);
|
|
String response = HttpRequest.post("http://127.0.0.1:1224/api/doc/upload")
|
.contentType("multipart/form-data; boundary=<calculated when request is sent>")
|
.form("file", file)
|
.execute()
|
.body();
|
|
System.out.println(response);
|
|
|
JSONObject json = JSONUtil.parseObj(response);
|
|
|
|
// System.out.println(json.get("data"));
|
|
|
|
|
|
return AjaxResult.success(json);
|
// unicodeEscapeToChinese(response);
|
// System.out.println(res);
|
// unicodeEscapeToChinese(res);
|
|
}
|
|
@Override
|
public AjaxResult getDownLoadInfo(String id) {
|
JSONObject inputObject = new JSONObject();
|
inputObject.put("id", id);
|
String res = HttpRequest.post("http://127.0.0.1:1224/api/doc/download")
|
// .contentType("multipart/form-data; boundary=<calculated when request is sent>")
|
//.form("file", file)
|
.body(inputObject.toString())
|
.contentType("application/json")
|
.execute()
|
.body();
|
JSONObject json = JSONUtil.parseObj(res);
|
return AjaxResult.success(json);
|
|
}
|
|
public void doublePdfGenerate() throws IOException {
|
// String ocrUrl = "http://localhost:32009/umi/api/ocr";
|
File pdfFile = new File("34.pdf");
|
String base64 = java.util.Base64.getEncoder().encodeToString(Files.readAllBytes(pdfFile.toPath()));
|
|
JSONObject inputObject = new JSONObject();
|
inputObject.put("base64", base64);
|
//inputObject.put("options", new JSONObject().put("ocr.language", "models/config_chinese.txt"));
|
|
String response = HttpRequest.post(OCRServer+"/umi/api/ocr")
|
.body(inputObject.toString())
|
.contentType("application/json")
|
.execute()
|
.body();
|
|
System.out.println("识别结果: " + response);
|
|
// 创建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());
|
}
|
}
|