调解系统后端服务

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.ruoyi.wisdomarbitrate.utils;
  2. import com.documents4j.api.DocumentType;
  3. import com.documents4j.api.IConverter;
  4. import com.documents4j.job.LocalConverter;
  5. import com.tencentcloudapi.common.Credential;
  6. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  7. import com.tencentcloudapi.common.profile.ClientProfile;
  8. import com.tencentcloudapi.common.profile.HttpProfile;
  9. import com.tencentcloudapi.ocr.v20181119.OcrClient;
  10. import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest;
  11. import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse;
  12. import com.tencentcloudapi.ocr.v20181119.models.SmartStructuralOCRV2Request;
  13. import com.tencentcloudapi.ocr.v20181119.models.SmartStructuralOCRV2Response;
  14. import org.apache.pdfbox.pdmodel.PDDocument;
  15. import org.json.JSONArray;
  16. import org.json.JSONObject;
  17. import java.io.*;
  18. import java.util.*;
  19. public class Tset {
  20. public static void main(String[] args) {
  21. //API的SecretId
  22. final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp";
  23. //API的SecretKey
  24. final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V";
  25. // String pdfFilePath = "http://121.40.189.20:9000/API/uploadPath/upload/ca2ca4697e5449ff9b9d23f95b221f58.pdf";
  26. // String pdfFilePath = "http://121.40.189.20:9000/API/uploadPath/upload/2023/11/14/ca2ca4697e5449ff9b9d23f95b221f58.pdf";
  27. String pdfFilePath ="D:/home/unzip/b92a7291-441e-4fad-95b4-7305f206eabd/仲裁材料/二、案件基本材料/2-1 仲裁申请书.pdf";
  28. try{
  29. // 读取文件内容到字节数组
  30. byte[] fileBytes = readFileToBytes(pdfFilePath);
  31. // 将字节数组转换为Base64值
  32. String base64String = encodeBytesToBase64(fileBytes);
  33. // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
  34. // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
  35. // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
  36. Credential cred = new Credential(SECRET_ID, SECRET_KEY);
  37. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  38. HttpProfile httpProfile = new HttpProfile();
  39. httpProfile.setEndpoint("ocr.tencentcloudapi.com");
  40. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  41. ClientProfile clientProfile = new ClientProfile();
  42. clientProfile.setHttpProfile(httpProfile);
  43. // 实例化要请求产品的client对象,clientProfile是可选的
  44. OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
  45. // 实例化一个请求对象,每个接口都会对应一个request对象
  46. SmartStructuralOCRV2Request req = new SmartStructuralOCRV2Request();
  47. req.setImageBase64(base64String);
  48. req.setIsPdf(true);
  49. req.setPdfPageNumber(1L);
  50. String[] itemNames1 = {"申请人", "统一社会信用代码", "负责人", "住所", "联系地址"
  51. , "委托代理人", "联系电话", "电子邮件", "被申请人", "居民身份证号码", "仲裁请求", "事实和理由"};
  52. req.setItemNames(itemNames1);
  53. // 返回的resp是一个SmartStructuralOCRV2Response的实例,与请求对象对应
  54. SmartStructuralOCRV2Response resp = client.SmartStructuralOCRV2(req);
  55. //解析数据
  56. String s = SmartStructuralOCRV2Response.toJsonString(resp);
  57. // 解析JSON数据
  58. JSONObject jsonObject = new JSONObject(s);
  59. JSONArray structuralList = jsonObject.getJSONArray("StructuralList");
  60. // 遍历StructuralList中的Groups,获取Key对应的AutoName和Value对应的AutoContent
  61. StringBuilder stringBuilder = new StringBuilder(); // 创建一个StringBuilder对象
  62. for (int i = 0; i < structuralList.length(); i++) {
  63. JSONArray groups = structuralList.getJSONObject(i).getJSONArray("Groups");
  64. for (int j = 0; j < groups.length(); j++) {
  65. JSONArray lines = groups.getJSONObject(j).getJSONArray("Lines");
  66. for (int k = 0; k < lines.length(); k++) {
  67. JSONObject line = lines.getJSONObject(k);
  68. JSONObject key = line.getJSONObject("Key");
  69. JSONObject value = line.getJSONObject("Value");
  70. String autoName = key.getString("AutoName");
  71. String autoContent = value.getString("AutoContent");
  72. String text = autoName + ":" + autoContent;
  73. if (stringBuilder.length() > 0) {
  74. stringBuilder.append(","); // 在已有内容的情况下添加逗号分隔符
  75. }
  76. stringBuilder.append(text); // 拼接当前的字符串
  77. }
  78. }
  79. }
  80. System.out.println("stringBuilder======"+stringBuilder.toString());
  81. // 将字符串按逗号分隔符切割
  82. String[] fields = stringBuilder.toString().split(",");
  83. Map<String, List<String>> map = new HashMap<>();
  84. for (String field : fields) {
  85. // 对于每个字段,再按冒号分隔符拆分出键和值
  86. String[] keyValue = field.split(":");
  87. if (keyValue.length == 2) { // 对于合法的键值对,将其添加到JSON对象中
  88. String key = keyValue[0];
  89. String value = keyValue[1];
  90. // 判断Map中是否已存在该键
  91. if (map.containsKey(key)) {
  92. // 如果已存在,获取该键对应的值,并将新的值添加到集合中
  93. List<String> values = map.get(key);
  94. values.add(value);
  95. } else {
  96. // 如果不存在,创建一个新的集合,并将值添加到集合中
  97. List<String> values = new ArrayList<>();
  98. values.add(value);
  99. map.put(key, values);
  100. }
  101. }
  102. }
  103. System.out.println(map);
  104. } catch (Exception e) {
  105. System.out.println(e.toString());
  106. }
  107. }
  108. private static byte[] readFileToBytes(String filePath) throws IOException {
  109. File file = new File(filePath);
  110. FileInputStream fileInputStream = new FileInputStream(file);
  111. byte[] fileBytes = new byte[(int) file.length()];
  112. fileInputStream.read(fileBytes);
  113. fileInputStream.close();
  114. return fileBytes;
  115. }
  116. private static String encodeBytesToBase64(byte[] bytes) {
  117. return Base64.getEncoder().encodeToString(bytes);
  118. }
  119. }