Commit 1a470204 by 黄明步

修改文件下载

parent 9cf8b1c5
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;
/**
* 接口转发控制器
......@@ -49,114 +31,6 @@ public class RetransmissionController {
@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();
......@@ -177,22 +51,26 @@ public class RetransmissionController {
@PostMapping("/ofs/api/sync/v1/pdf")
public void downloadDoubleDeckPdf(@RequestBody JSONObject body, HttpServletRequest request, HttpServletResponse response) {
retransmissionService.downloadDoubleDeckPdf(body, request, response);
// retransmissionService.downloadDoubleDeckPdf(body, request, response);
retransmissionService.downloadPdfNew(body, response);
}
@PostMapping("/ofs/api/sync/v1/ft/pdf")
public void downloadDoubleDeckPdfByFt(@RequestBody JSONObject body, HttpServletRequest request, HttpServletResponse response) {
retransmissionService.downloadDoubleDeckPdf(body, request, response);
// retransmissionService.downloadDoubleDeckPdf(body, request, response);
retransmissionService.downloadPdfNew(body, response);
}
@PostMapping("/ofs/api/sync/v1/odf")
@PostMapping("/ofs/api/sync/v1/ofd")
public void downloadDoubleDeckOfd(@RequestBody JSONObject body, HttpServletRequest request, HttpServletResponse response) {
retransmissionService.downloadDoubleDeckOfd(body, request, response);
// retransmissionService.downloadDoubleDeckOfd(body, request, response);
retransmissionService.downloadOfdNew(body, response);
}
@PostMapping("/ofs/api/sync/v1/ft/odf")
@PostMapping("/ofs/api/sync/v1/ft/ofd")
public void downloadDoubleDeckOfdByFt(@RequestBody JSONObject body, HttpServletRequest request, HttpServletResponse response) {
retransmissionService.downloadDoubleDeckOfd(body, request, response);
// retransmissionService.downloadDoubleDeckOfd(body, request, response);
retransmissionService.downloadOfdNew(body, response);
}
@PostMapping("/ofs/api/sync/v1/ft/1001")
......
package com.gxmailu.ocrCloudPlatform.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
/**
* 应用调用能力记录临时中间表
*
* @author Hmb
* @since 2023/12/22 16:44
*/
@TableName("tmp_record_by_trigger")
public class TmpRecordByTrigger extends AppAbilityRecord implements Serializable {
private static final long serialVersionUID = 1L;
}
package com.gxmailu.ocrCloudPlatform.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxmailu.ocrCloudPlatform.entity.TmpRecordByTrigger;
import org.springframework.stereotype.Repository;
/**
* @author Hmb
* @since 2023/12/22 16:45
*/
@Repository
public interface TmpRecordByTriggerMapper extends BaseMapper<TmpRecordByTrigger> {
}
package com.gxmailu.ocrCloudPlatform.service.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
......@@ -24,8 +26,10 @@ import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@Service
@Slf4j
......@@ -144,9 +148,10 @@ public class MessageServiceImpl implements MessageService {
String title = alerts.getJSONObject("labels").getString("alertname");
String body = alerts.getJSONObject("annotations").getString("summary");
String dateStr = alerts.getString("startsAt");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = df.parse(dateStr);
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// df.setTimeZone(TimeZone.getTimeZone("UTC"));
// Date date = df.parse(dateStr);
DateTime date = DateUtil.parse(dateStr);
Message dto = new Message();
dto.setTitle(title);
dto.setBody(body);
......
package com.gxmailu.ocrCloudPlatform.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
......@@ -20,6 +22,7 @@ import com.gxmailu.ocrCloudPlatform.config.DistributedRedisLock;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.entity.ServerInfo;
import com.gxmailu.ocrCloudPlatform.entity.TmpRecordByTrigger;
import com.gxmailu.ocrCloudPlatform.mapper.*;
import com.gxmailu.ocrCloudPlatform.service.ServerInfoService;
import com.gxmailu.ocrCloudPlatform.vo.OcrResult;
......@@ -38,6 +41,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -70,6 +74,8 @@ public class RetransmissionService {
private AppAbilityRecord9Mapper record9Mapper;
@Autowired
private AppAbilityRecordAllMapper recordAllMapper;
@Autowired
private TmpRecordByTriggerMapper tmpRecordByTriggerMapper;
@Autowired
private ServerInfoMapper serverInfoMapper;
......@@ -164,9 +170,9 @@ public class RetransmissionService {
public OcrResult commonRecognition(String code, String fileId, MultipartFile file, Boolean createOfd, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
// 不能直接传 MultipartFile,另存一份临时文件,请求完毕后删除。
// File tempFile = new File("C:\\Users\\Administrator\\Desktop\\test", System.currentTimeMillis() +"_"+ file.getOriginalFilename());
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + FileUtil.getName(file.getOriginalFilename()));
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
try {
file.transferTo(tempFile);
} catch (IOException e) {
......@@ -174,13 +180,11 @@ public class RetransmissionService {
}
paramMap.put("file", tempFile);
paramMap.put("createOfd", createOfd);
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
// setOcrTask(ocrServerAddress.getIp());
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -196,16 +200,15 @@ public class RetransmissionService {
// 记录请求结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -213,9 +216,9 @@ public class RetransmissionService {
public OcrResult commonRecognitionV2(String code, String fileId, MultipartFile file, Boolean createOfd, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
// 不能直接传 MultipartFile,另存一份临时文件,请求完毕后删除。
// File tempFile = new File("C:\\Users\\Administrator\\Desktop\\test", System.currentTimeMillis() +"_"+ file.getOriginalFilename());
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + FileUtil.getName(file.getOriginalFilename()));
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
try {
file.transferTo(tempFile);
} catch (IOException e) {
......@@ -247,11 +250,11 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -288,36 +291,47 @@ public class RetransmissionService {
}
public void downloadPdfNew(JSONObject body, HttpServletResponse response) {
Long id = body.getLong("sid");
try {
Thread.sleep(50);
JSONObject info = JSONUtil.parseObj(body);
Long id = info.getLong("sid");
if (id == null) {
log.error("请求体中缺少 'sid' 字段,无法进行后续处理");
return;
}
AppAbilityRecordAll appAbilityRecordAll = elasticSearchService.getDocumentById(id.toString());
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从es中获取文件数据为空");
// 兜底方案直接查询数据库
appAbilityRecordAll = recordAllMapper.selectById(id);
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从数据库中获取文件数据为空");
// 如果 appAbilityRecordAll 为空,或者相关字段为空,则直接查询数据库
if (appAbilityRecordAll == null || StrUtil.isBlank(appAbilityRecordAll.getServerIp())
|| StrUtil.isBlank(appAbilityRecordAll.getDstUrl())
|| StrUtil.isBlank(appAbilityRecordAll.getPdf())) {
TmpRecordByTrigger tmpRecordByTrigger = tmpRecordByTriggerMapper.selectById(id);
if (tmpRecordByTrigger == null) {
log.error("从查询不到ID为 {} 的文件数据", id);
return;
}
appAbilityRecordAll = new AppAbilityRecordAll();
BeanUtil.copyProperties(tmpRecordByTriggerMapper.selectById(id), appAbilityRecordAll);
}
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下载文件");
// 如果再次查询后结果仍为空,中止下载处理
if (StrUtil.isBlank(appAbilityRecordAll.getServerIp())
|| StrUtil.isBlank(appAbilityRecordAll.getDstUrl())
|| StrUtil.isBlank(appAbilityRecordAll.getPdf())) {
log.error("无法获取ID为 {} 的完整文件信息,中止下载处理", id);
return;
}
// 构建MinIO文件的URL
String fileUrl = "http://" + serverIp + ":9000/" + dstUrl + "/" + objName;
// 构建MinIO文件的URL
String serverIp = appAbilityRecordAll.getServerIp();
String dstUrl = appAbilityRecordAll.getDstUrl();
String objName = appAbilityRecordAll.getPdf();
String fileUrl = StrUtil.format("http://{}:9000/{}/{}", serverIp, dstUrl, objName);
byte[] bytes = HttpUtil.downloadBytes(fileUrl);
response.getOutputStream().write(bytes);
try (OutputStream outputStream = response.getOutputStream()) {
outputStream.write(bytes);
}
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e);
returnError(response, 500, "OCR云平台接口异常");
}
}
......@@ -349,38 +363,48 @@ public class RetransmissionService {
}
}
public void downloadOfdNew(JSONObject body, HttpServletResponse response) {
Long id = body.getLong("sid");
try {
Thread.sleep(50);
JSONObject info = JSONUtil.parseObj(body);
Long id = info.getLong("sid");
if (id == null) {
log.error("请求体中缺少 'sid' 字段,无法进行后续处理");
return;
}
AppAbilityRecordAll appAbilityRecordAll = elasticSearchService.getDocumentById(id.toString());
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从es中获取文件数据为空");
// 兜底方案直接查询数据库
appAbilityRecordAll = recordAllMapper.selectById(id);
if (ObjUtil.isNull(appAbilityRecordAll)) {
log.error("从数据库中获取文件数据为空");
// 如果 appAbilityRecordAll 为空,或者相关字段为空,再次查询数据库
if (appAbilityRecordAll == null || StrUtil.isBlank(appAbilityRecordAll.getServerIp())
|| StrUtil.isBlank(appAbilityRecordAll.getDstUrl())
|| StrUtil.isBlank(appAbilityRecordAll.getPdf())) {
TmpRecordByTrigger tmpRecordByTrigger = tmpRecordByTriggerMapper.selectById(id);
if (tmpRecordByTrigger == null) {
log.error("从数据库中无法获取ID为 {} 的文件数据", id);
return;
}
BeanUtil.copyProperties(tmpRecordByTriggerMapper.selectById(id), appAbilityRecordAll);
}
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下载文件");
// 如果再次查询后结果仍为空,中止下载处理
if (appAbilityRecordAll == null || StrUtil.isBlank(appAbilityRecordAll.getServerIp())
|| StrUtil.isBlank(appAbilityRecordAll.getDstUrl())
|| StrUtil.isBlank(appAbilityRecordAll.getPdf())) {
log.error("无法获取ID为 {} 的完整文件信息,中止下载处理", id);
return;
}
objName = objName.substring(0, objName.lastIndexOf(".")) + ".ofd";
// 构建MinIO文件的URL
String fileUrl = "http://" + serverIp + ":9000/" + dstUrl + "/" + objName;
String serverIp = appAbilityRecordAll.getServerIp();
String dstUrl = appAbilityRecordAll.getDstUrl();
String objName = appAbilityRecordAll.getPdf().substring(0, appAbilityRecordAll.getPdf().lastIndexOf(".")) + ".ofd";
String fileUrl = StrUtil.format("http://{}:9000/{}/{}", serverIp, dstUrl, objName);
byte[] bytes = HttpUtil.downloadBytes(fileUrl);
response.getOutputStream().write(bytes);
try (OutputStream outputStream = response.getOutputStream()) {
outputStream.write(bytes);
}
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e);
returnError(response, 500, "OCR云平台接口异常");
}
}
......@@ -388,20 +412,20 @@ public class RetransmissionService {
public OcrResult fullTextRecognition(String fileId, MultipartFile file, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + FileUtil.getName(file.getOriginalFilename()));
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
// setOcrTask(ocrServerAddress.getIp());
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -417,16 +441,15 @@ public class RetransmissionService {
// 设置结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -434,20 +457,20 @@ public class RetransmissionService {
public OcrResult fullTextRecognitionV2(String fileId, MultipartFile file, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + FileUtil.getName(file.getOriginalFilename()));
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
// setOcrTask(ocrServerAddress.getIp());
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -463,16 +486,15 @@ public class RetransmissionService {
// 设置结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -480,20 +502,20 @@ public class RetransmissionService {
public OcrResult cardRecognition(Integer code, String fileId, MultipartFile file, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + FileUtil.getName(file.getOriginalFilename()));
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
// setOcrTask(ocrServerAddress.getIp());
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -508,16 +530,15 @@ public class RetransmissionService {
// 设置结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -531,13 +552,11 @@ public class RetransmissionService {
body.set("name", name);
body.set("callback_url", callback_url);
body.set("need_pdf", need_pdf);
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
// setOcrTask(ocrServerAddress.getIp());
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -553,7 +572,6 @@ public class RetransmissionService {
// 设置结束时间
long endTime = System.currentTimeMillis();
// Assert.hasText(res, "请求接口返回为空");
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
......@@ -562,7 +580,7 @@ public class RetransmissionService {
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), id);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -570,20 +588,21 @@ public class RetransmissionService {
public OcrResult formatRestoration(String fileId, MultipartFile file, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + FileUtil.getName(file.getOriginalFilename()));
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
// setOcrTask(ocrServerAddress.getIp());
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -602,13 +621,12 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
// Assert.hasText(res, "请求接口返回为空");
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp());
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
}
......@@ -617,7 +635,9 @@ public class RetransmissionService {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
paramMap.put("dstFileType", dstFileType);
File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + srcFile.getOriginalFilename());
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(srcFile.getOriginalFilename())));
try {
srcFile.transferTo(tempFile);
} catch (IOException e) {
......@@ -648,7 +668,7 @@ public class RetransmissionService {
response.setHeader(entry.getKey(), entry.getValue().get(0));
}
}
FileUtil.del(tempFile);
FileUtil.del(tempSaveDir);
addServerRequestCount(ocrServerAddress.getIp(), -1);
// 赋值body
IoUtil.copy(httpResponse.bodyStream(), response.getOutputStream());
......@@ -660,11 +680,11 @@ public class RetransmissionService {
}
@Async("dataPersistThreadPoolTaskExecutor")
public void saveToAppAbilityRecordAll(OcrResult result, HttpServletRequest request, String serverIp) {
public void saveToAppAbilityRecordAll(OcrResult result, HttpServletRequest request, String serverIp, String fileId) {
try {
String clientIP = ServletUtil.getClientIP(request);
if (Objects.equals(result.getCode(), "000000")) {
log.info("调用成功,开始持久化存储...");
// log.info("调用成功,开始持久化存储...");
JSONObject info = JSONUtil.parseObj(result.getData());
Long id = info.getLong("sid");
......@@ -678,7 +698,7 @@ public class RetransmissionService {
handleRecord(record8Mapper, id, clientIP, serverIp);
handleRecord(record9Mapper, id, clientIP, serverIp);
} else {
log.error("请求OCR接口错误{}", result);
log.error("请求OCR接口错误{}, fileId={}", result, fileId);
}
} catch (Exception e) {
log.error("数据{}保存发生异常!", result, e);
......@@ -691,7 +711,7 @@ public class RetransmissionService {
return;
}
record.setIp(clientIP);
log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record));
// log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record));
AppAbilityRecordAll recordAll = recordAllMapper.selectById(id);
if (recordAll == null) {
recordAll = new AppAbilityRecordAll();
......@@ -701,7 +721,7 @@ public class RetransmissionService {
if (StrUtil.isEmpty(recordAll.getServerIp())) {
recordAll.setServerIp(serverIp);
}
log.info("添加id:{} 的数据到recordAll持久化保存结果:{}", recordAll.getId(), recordAllMapper.insert(recordAll));
// log.info("添加id:{} 的数据到recordAll持久化保存结果:{}", recordAll.getId(), recordAllMapper.insert(recordAll));
} else {
AppAbilityRecordAll newRecordAll = new AppAbilityRecordAll();
BeanUtils.copyProperties(record, newRecordAll, AppAbilityRecordAll.class);
......@@ -709,7 +729,7 @@ public class RetransmissionService {
if (StrUtil.isEmpty(newRecordAll.getServerIp())) {
newRecordAll.setServerIp(serverIp);
}
log.info("修改recordAll中id:{} 的数据结果:{}", newRecordAll.getId(), recordAllMapper.updateById(newRecordAll));
// 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