Commit 718b32b8 by 黄明步

1.引入es,修改文件获取方式,从es查询文件数据,拼接地址下载文件

2.为了兼容es,将时间类型统一修改为Date
parent c55906ff
......@@ -19,6 +19,7 @@
<properties>
<java.version>8</java.version>
<spring-cloud.version>Hoxton.SR11</spring-cloud.version>
<elasticsearch.version>8.9.1</elasticsearch.version>
</properties>
<dependencyManagement>
<dependencies>
......@@ -176,6 +177,18 @@
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.6</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
......
package com.gxmailu.ocrCloudPlatform.config;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
* @author Hmb
* @since 2023/9/04 08:44
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchClient {
private String hosts;
private Integer port;
private String http;
private String username;
private String password;
/**
* 获取elasticsearch客户端
*
* @return {@link co.elastic.clients.elasticsearch.ElasticsearchClient}
*/
@Bean
public co.elastic.clients.elasticsearch.ElasticsearchClient getElasticsearchClient() {
HttpHost[] hostArray = Arrays.stream(hosts.split(","))
.map(host -> new HttpHost(host, port, http))
.toArray(HttpHost[]::new);
// 配置密码认证
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
// 创建 HTTP 客户端配置
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(60000) // 设置连接超时时间(毫秒)
.setSocketTimeout(300000) // 设置套接字超时时间(毫秒)
.build();
RestClient restClient = RestClient.builder(hostArray)
.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder
.setDefaultCredentialsProvider(credentialsProvider)
.setDefaultRequestConfig(requestConfig)
// 显式设置keepAliveStrategy
.setKeepAliveStrategy((httpResponse,httpContext) -> TimeUnit.MINUTES.toMillis(3))
// 显式开启tcp keepalive
.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(true).build())
).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
return new co.elastic.clients.elasticsearch.ElasticsearchClient(transport);
}
}
package com.gxmailu.ocrCloudPlatform.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecord1Mapper;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.service.impl.ElasticSearchService;
import com.gxmailu.ocrCloudPlatform.service.impl.RetransmissionService;
import com.gxmailu.ocrCloudPlatform.vo.OcrResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;
/**
* 接口转发控制器
*/
@Slf4j
@RestController
//@RequestMapping("/retransmission")
public class RetransmissionController {
@Autowired
private RetransmissionService retransmissionService;
@Autowired
private ElasticSearchService elasticSearchService;
@Autowired
private AppAbilityRecord1Mapper record1Mapper;
@Autowired
private AppAbilityRecordAllMapper recordAllMapper;
@GetMapping("/test")
public void test(HttpServletResponse response) throws IOException {
OcrResult result = rec();
JSONObject info = JSONUtil.parseObj(result.getData());
Long id = info.getLong("sid");
AppAbilityRecordAll appAbilityRecordAll = elasticSearchService.getDocumentById(id.toString());
if (ObjUtil.isNull(appAbilityRecordAll)) {
System.out.println("从es中获取的数据为空");
appAbilityRecordAll = recordAllMapper.selectById(id);
if (ObjUtil.isNull(appAbilityRecordAll)) {
System.out.println("查询数据库也为空");
} else {
System.out.println(appAbilityRecordAll);
}
} else {
System.out.println("从es中取到数据:" + appAbilityRecordAll);
String serverIp = appAbilityRecordAll.getServerIp();
String dstUrl = appAbilityRecordAll.getDstUrl();
String pdf = appAbilityRecordAll.getPdf();
// 构建MinIO文件的URL
String fileUrl = "http://" + serverIp + ":9000/" + dstUrl + "/" + pdf;
try {
byte[] bytes = HttpUtil.downloadBytes(fileUrl);
response.getOutputStream().write(bytes);
} catch (IOException e) {
// 处理异常
e.printStackTrace();
}
}
}
private OcrResult rec() {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", IdUtil.fastSimpleUUID());
File tempFile = new File("C:\\Users\\Windows11\\Desktop\\ll\\0239.jpg");
paramMap.put("file", tempFile);
String ocrApi = "http://127.0.0.1:18082";
// 设置开始时间
long startTime = System.currentTimeMillis();
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ft/1001")
.header("Authorization", "AppId aj32jafj-l948-0034-zn6k-6dqymid9le32")
.header("X-Timestamp", "1685889614304")
.header("X-Checksum", "d57ed307c05f1088a3216f2031a9b11c")
.form(paramMap).execute().body();
// 设置结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
// return new OcrResult("-1", "请求失败", null, null);
}
// FileUtil.del(tempFile);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
saveToAppAbilityRecordAll(result, "127.0.0.1");
return result;
}
@Async("dataPersistThreadPoolTaskExecutor")
public void saveToAppAbilityRecordAll(OcrResult result, String serverIp) {
try {
if (Objects.equals(result.getCode(), "000000")) {
log.info("调用成功,开始持久化存储...");
JSONObject info = JSONUtil.parseObj(result.getData());
Long id = info.getLong("sid");
handleRecord(record1Mapper, id, "127.0.0.1", serverIp);
} else {
log.error("请求OCR接口错误{}", result);
}
} catch (Exception e) {
log.error("数据{}保存发生异常!", result, e);
}
}
private <T extends AppAbilityRecord> void handleRecord(BaseMapper<T> mapper, Long id, String clientIP, String serverIp) {
T record = mapper.selectById(id);
if (ObjUtil.isNull(record)) {
return;
}
record.setIp(clientIP);
log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record));
AppAbilityRecordAll recordAll = recordAllMapper.selectById(id);
if (recordAll == null) {
recordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, recordAll, AppAbilityRecordAll.class);
recordAll.setCreatedTime(DateUtil.date());
recordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(recordAll.getServerIp())) {
recordAll.setServerIp(serverIp);
}
log.info("添加id:{} 的数据到recordAll持久化保存结果:{}", recordAll.getId(), recordAllMapper.insert(recordAll));
} else {
AppAbilityRecordAll newRecordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, newRecordAll, AppAbilityRecordAll.class);
newRecordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(newRecordAll.getServerIp())) {
newRecordAll.setServerIp(serverIp);
}
log.info("修改recordAll中id:{} 的数据结果:{}", newRecordAll.getId(), recordAllMapper.updateById(newRecordAll));
}
}
@GetMapping("/ofs/api/sync/getClientOcrTask")
public OcrResult getClientOcrTask() {
return this.retransmissionService.getClientOcrTask();
......
......@@ -7,6 +7,7 @@ import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
/**
* <p>
......@@ -61,7 +62,7 @@ public class AppAbilityRecord implements Serializable {
* 调用时间
*/
@TableField("call_time")
private LocalDateTime callTime;
private Date callTime;
/**
* 识别状态
......@@ -163,13 +164,13 @@ public class AppAbilityRecord implements Serializable {
* 创建时间
*/
@TableField("created_time")
private LocalDateTime createdTime;
private Date createdTime;
/**
* 更新时间
*/
@TableField("updated_time")
private LocalDateTime updatedTime;
private Date updatedTime;
@TableField("server_ip")
private String serverIp;
......
......@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
/**
* <p>
......@@ -61,7 +62,7 @@ public class AppAbilityRecordAll implements Serializable {
* 调用时间
*/
@TableField("call_time")
private LocalDateTime callTime;
private Date callTime;
/**
* 识别状态
......@@ -163,13 +164,13 @@ public class AppAbilityRecordAll implements Serializable {
* 创建时间
*/
@TableField("created_time")
private LocalDateTime createdTime;
private Date createdTime;
/**
* 更新时间
*/
@TableField("updated_time")
private LocalDateTime updatedTime;
private Date updatedTime;
@TableField("server_ip")
private String serverIp;
......@@ -228,11 +229,11 @@ public class AppAbilityRecordAll implements Serializable {
this.ip = ip;
}
public LocalDateTime getCallTime() {
public Date getCallTime() {
return callTime;
}
public void setCallTime(LocalDateTime callTime) {
public void setCallTime(Date callTime) {
this.callTime = callTime;
}
......@@ -364,19 +365,19 @@ public class AppAbilityRecordAll implements Serializable {
this.callbackParams = callbackParams;
}
public LocalDateTime getCreatedTime() {
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(LocalDateTime createdTime) {
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public LocalDateTime getUpdatedTime() {
public Date getUpdatedTime() {
return updatedTime;
}
public void setUpdatedTime(LocalDateTime updatedTime) {
public void setUpdatedTime(Date updatedTime) {
this.updatedTime = updatedTime;
}
......
package com.gxmailu.ocrCloudPlatform.service.impl;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.DeleteByQueryResponse;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author Hmb
* @since 2023/9/04 08:43
*/
@Slf4j
@Service
public class ElasticSearchService{
@Autowired
private ElasticsearchClient client;
private static final String APP_ABILITY_RECORD_INDEX = "app_ability_record";
private static final Map<Character, String> SPECIAL_CHARACTERS_MAP = new HashMap<>();
static {
SPECIAL_CHARACTERS_MAP.put('[', "\\[");
SPECIAL_CHARACTERS_MAP.put(']', "\\]");
SPECIAL_CHARACTERS_MAP.put('(', "\\(");
SPECIAL_CHARACTERS_MAP.put(')', "\\)");
SPECIAL_CHARACTERS_MAP.put('+', "\\+");
SPECIAL_CHARACTERS_MAP.put('-', "\\-");
SPECIAL_CHARACTERS_MAP.put('?', "\\?");
SPECIAL_CHARACTERS_MAP.put('!', "\\!");
SPECIAL_CHARACTERS_MAP.put('\\', "\\\\");
// 添加更多特殊字符和其转义规则
}
/**
* 转义特殊字符
*
* @param input
* @return
*/
public static String escapeSpecialCharacters(String input) {
StringBuilder escaped = new StringBuilder();
for (char c : input.toCharArray()) {
if (SPECIAL_CHARACTERS_MAP.containsKey(c)) {
escaped.append(SPECIAL_CHARACTERS_MAP.get(c));
} else {
escaped.append(c);
}
}
return escaped.toString();
}
public AppAbilityRecordAll getDocumentById(String id) throws IOException {
GetResponse<AppAbilityRecordAll> response = client.get(getRequest -> getRequest.id(id).index(APP_ABILITY_RECORD_INDEX), AppAbilityRecordAll.class);
return response.source();
}
public void deleteIndex(String index) throws IOException {
DeleteIndexResponse deleteIndexResponse = client.indices().delete(s -> s.index(index));
log.info("删除索引操作结果:{}", deleteIndexResponse.acknowledged());
}
public void deleteAllDoc(String index) throws IOException {
DeleteByQueryResponse deleteByQueryResponse = client.deleteByQuery(s -> s.index(index).query(q -> q.matchAll(m -> m)));
log.info("删除索引文档操作结果:{}",deleteByQueryResponse.deleted());
}
}
......@@ -10,6 +10,7 @@ import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
......@@ -82,6 +83,9 @@ public class RetransmissionService {
@Autowired
private DistributedRedisLock distributedRedisLock;
@Autowired
private ElasticSearchService elasticSearchService;
@Value("${temp.dir}")
private String tempDir;
......@@ -288,6 +292,41 @@ public class RetransmissionService {
}
}
public void downloadPdfNew(JSONObject body, HttpServletResponse response) {
try {
Thread.sleep(50);
JSONObject info = JSONUtil.parseObj(body);
Long id = info.getLong("sid");
AppAbilityRecordAll appAbilityRecordAll = elasticSearchService.getDocumentById(id.toString());
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从es中获取文件数据为空");
// 兜底方案直接查询数据库
appAbilityRecordAll = recordAllMapper.selectById(id);
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从数据库中获取文件数据为空");
return;
}
}
if (ObjUtil.isNotNull(appAbilityRecordAll)) {
String serverIp = appAbilityRecordAll.getServerIp();
String dstUrl = appAbilityRecordAll.getDstUrl();
String objName = appAbilityRecordAll.getPdf();
if (StrUtil.isBlank(serverIp) || StrUtil.isBlank(dstUrl) || StrUtil.isBlank(objName)) {
log.error("获取到数据内容不全,无法拼接URL下载文件");
return;
}
// 构建MinIO文件的URL
String fileUrl = "http://" + serverIp + ":9000/" + dstUrl + "/" + objName;
byte[] bytes = HttpUtil.downloadBytes(fileUrl);
response.getOutputStream().write(bytes);
}
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
returnError(response, 500, "OCR云平台接口异常");
}
}
public void downloadDoubleDeckOfd(JSONObject body, HttpServletRequest request, HttpServletResponse response) {
try {
// ServerInfo ocrServerAddress = getOcrServerAddress();
......@@ -317,7 +356,42 @@ public class RetransmissionService {
log.error("OCR云平台接口异常", e);
returnError(response, 500, "OCR云平台接口异常");
}
}
public void downloadOfdNew(JSONObject body, HttpServletResponse response) {
try {
Thread.sleep(50);
JSONObject info = JSONUtil.parseObj(body);
Long id = info.getLong("sid");
AppAbilityRecordAll appAbilityRecordAll = elasticSearchService.getDocumentById(id.toString());
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从es中获取文件数据为空");
// 兜底方案直接查询数据库
appAbilityRecordAll = recordAllMapper.selectById(id);
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从数据库中获取文件数据为空");
return;
}
}
if (ObjUtil.isNotNull(appAbilityRecordAll)) {
String serverIp = appAbilityRecordAll.getServerIp();
String dstUrl = appAbilityRecordAll.getDstUrl();
String objName = appAbilityRecordAll.getPdf();
if (StrUtil.isBlank(serverIp) || StrUtil.isBlank(dstUrl) || StrUtil.isBlank(objName)) {
log.error("获取到数据内容不全,无法拼接URL下载文件");
return;
}
objName = objName.substring(0, objName.lastIndexOf(".")) + ".ofd";
// 构建MinIO文件的URL
String fileUrl = "http://" + serverIp + ":9000/" + dstUrl + "/" + objName;
byte[] bytes = HttpUtil.downloadBytes(fileUrl);
response.getOutputStream().write(bytes);
}
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
returnError(response, 500, "OCR云平台接口异常");
}
}
public OcrResult fullTextRecognition(String fileId, MultipartFile file, HttpServletRequest request) {
......@@ -631,8 +705,8 @@ public class RetransmissionService {
if (recordAll == null) {
recordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, recordAll, AppAbilityRecordAll.class);
recordAll.setCreatedTime(LocalDateTime.now());
recordAll.setUpdatedTime(LocalDateTime.now());
recordAll.setCreatedTime(DateUtil.date());
recordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(recordAll.getServerIp())) {
recordAll.setServerIp(serverIp);
}
......@@ -640,7 +714,7 @@ public class RetransmissionService {
} else {
AppAbilityRecordAll newRecordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, newRecordAll, AppAbilityRecordAll.class);
newRecordAll.setUpdatedTime(LocalDateTime.now());
newRecordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(newRecordAll.getServerIp())) {
newRecordAll.setServerIp(serverIp);
}
......
......@@ -35,4 +35,12 @@ ocr:
SMS:
url: http://147.2.3.18:9888/ums/outside_new/sendBatch
appid: ysyysbxt
appkey: M.%Cw!
\ No newline at end of file
appkey: M.%Cw!
# es集群
elasticsearch:
hosts: 147.1.5.133,147.1.5.134,147.1.5.135
port: 9200
http: http
username: elastic
password: vvOxUJVNwxoN4FeIQeYC
\ No newline at end of file
......@@ -57,6 +57,9 @@ mybatis:
temp:
dir: D:\data\ocrAdmin\template
eureka:
client:
enabled: false
\ No newline at end of file
elasticsearch:
hosts: 129.204.37.121
port: 9200
http: http
username: elastic
password: LBKsbjgEt+IECTA2CCz3
\ No newline at end of file
......@@ -10,18 +10,6 @@ spring:
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
# redis:
# host: 147.1.5.77
# port: 6379
# database: 1
# password: MaiLu/1301
# jedis:
# pool:
# max-active: 128
# max-wait: 60s
# max-idle: 64
# min-idle: 0
# timeout: 5000
server:
port: 9027
tomcat:
......@@ -68,4 +56,10 @@ SMS:
url: http://172.28.1.71:9888/ums/outside_new/sendBatch
# url: http://171.106.48.55:19891/ums/outside_new/sendBatch
appid: ysyysbxt
appkey: M.%Cw!
\ No newline at end of file
appkey: M.%Cw!
# 取消es健康检查
management:
health:
elasticsearch:
enabled: false
\ No newline at end of file
package com.gxmailu.ocrCloudPlatform;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecord1Mapper;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.service.impl.ElasticSearchService;
import com.gxmailu.ocrCloudPlatform.vo.OcrResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Objects;
/**
* @author Hmb
* @version 1.0.0
* @since 2023/12/10 13:51:25
*/
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class CanalSyncTest {
@Autowired
private ElasticSearchService elasticSearchService;
@Autowired
private AppAbilityRecord1Mapper record1Mapper;
@Autowired
private AppAbilityRecordAllMapper recordAllMapper;
@Test
public void test() throws IOException {
// AppAbilityRecordAll document = elasticSearchService.getDocumentById("1");
OcrResult result = rec();
JSONObject info = JSONUtil.parseObj(result.getData());
Long id = info.getLong("sid");
AppAbilityRecordAll appAbilityRecordAll = elasticSearchService.getDocumentById(id.toString());
if (ObjUtil.isNull(appAbilityRecordAll)) {
System.out.println("从es中获取的数据为空");
appAbilityRecordAll = recordAllMapper.selectById(id);
if (ObjUtil.isNull(appAbilityRecordAll)) {
System.out.println("查询数据库也为空");
} else {
System.out.println(appAbilityRecordAll);
}
} else {
System.out.println("从es中取到数据:" + appAbilityRecordAll);
String serverIp = appAbilityRecordAll.getServerIp();
String dstUrl = appAbilityRecordAll.getDstUrl();
String pdf = appAbilityRecordAll.getPdf();
String fileUrl = "http://" + serverIp + ":9000/" + dstUrl + "/" + pdf;
try {
// 构建MinIO文件的URL
// String fileUrl = "http://minio-server:9000/" + bucketName + "/" + objectName;
// 打开URL连接
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
// 获取文件流
InputStream fileStream = connection.getInputStream();
// 构建HTTP响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", pdf);
// return new ResponseEntity<>(new InputStreamResource(fileStream), headers, HttpStatus.OK);
} catch (IOException e) {
// 处理异常
e.printStackTrace();
// return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
private OcrResult rec() {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", IdUtil.fastSimpleUUID());
File tempFile = new File("C:\\Users\\Windows11\\Desktop\\ll\\0239.jpg");
paramMap.put("file", tempFile);
String ocrApi = "http://127.0.0.1:18082";
// 设置开始时间
long startTime = System.currentTimeMillis();
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ft/1001")
.header("Authorization", "AppId aj32jafj-l948-0034-zn6k-6dqymid9le32")
.header("X-Timestamp", "1685889614304")
.header("X-Checksum", "d57ed307c05f1088a3216f2031a9b11c")
.form(paramMap).execute().body();
// 设置结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
// return new OcrResult("-1", "请求失败", null, null);
}
// FileUtil.del(tempFile);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
saveToAppAbilityRecordAll(result, "127.0.0.1");
return result;
}
@Async("dataPersistThreadPoolTaskExecutor")
public void saveToAppAbilityRecordAll(OcrResult result, String serverIp) {
try {
if (Objects.equals(result.getCode(), "000000")) {
log.info("调用成功,开始持久化存储...");
JSONObject info = JSONUtil.parseObj(result.getData());
Long id = info.getLong("sid");
handleRecord(record1Mapper, id, "127.0.0.1", serverIp);
} else {
log.error("请求OCR接口错误{}", result);
}
} catch (Exception e) {
log.error("数据{}保存发生异常!", result, e);
}
}
private <T extends AppAbilityRecord> void handleRecord(BaseMapper<T> mapper, Long id, String clientIP, String serverIp) {
T record = mapper.selectById(id);
if (ObjUtil.isNull(record)) {
return;
}
record.setIp(clientIP);
log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record));
AppAbilityRecordAll recordAll = recordAllMapper.selectById(id);
if (recordAll == null) {
recordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, recordAll, AppAbilityRecordAll.class);
recordAll.setCreatedTime(DateUtil.date());
recordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(recordAll.getServerIp())) {
recordAll.setServerIp(serverIp);
}
log.info("添加id:{} 的数据到recordAll持久化保存结果:{}", recordAll.getId(), recordAllMapper.insert(recordAll));
} else {
AppAbilityRecordAll newRecordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, newRecordAll, AppAbilityRecordAll.class);
newRecordAll.setUpdatedTime(DateUtil.date());
if (StrUtil.isEmpty(newRecordAll.getServerIp())) {
newRecordAll.setServerIp(serverIp);
}
log.info("修改recordAll中id:{} 的数据结果:{}", newRecordAll.getId(), recordAllMapper.updateById(newRecordAll));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment