Commit 76615edf by 黄明步

修改文件转换

parent c7a81612
......@@ -173,14 +173,11 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
paramMap.put("createOfd", createOfd);
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
......@@ -204,13 +201,20 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
}
}
}
public OcrResult commonRecognitionV2(String code, String fileId, MultipartFile file, Boolean createOfd, HttpServletRequest request) {
......@@ -219,14 +223,11 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
paramMap.put("createOfd", createOfd);
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
......@@ -250,13 +251,20 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
}
}
}
......@@ -332,7 +340,7 @@ public class RetransmissionService {
}
} catch (Exception e) {
log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e);
returnError(response, 500, "OCR云平台接口异常");
// returnError(response, 500, "OCR云平台接口异常");
}
}
......@@ -405,7 +413,7 @@ public class RetransmissionService {
}
} catch (Exception e) {
log.error("在处理ID为 {} 的PDF下载过程中发生异常", id, e);
returnError(response, 500, "OCR云平台接口异常");
// returnError(response, 500, "OCR云平台接口异常");
}
}
......@@ -415,14 +423,12 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
}
......@@ -445,13 +451,20 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
}
}
}
public OcrResult fullTextRecognitionV2(String fileId, MultipartFile file, HttpServletRequest request) {
......@@ -460,16 +473,15 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -490,13 +502,20 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
}
}
}
public OcrResult cardRecognition(Integer code, String fileId, MultipartFile file, HttpServletRequest request) {
......@@ -505,16 +524,14 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
......@@ -534,13 +551,20 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
}
}
}
public OcrResult cardRecognitionBaseMinio(String id, String bucket,
......@@ -591,16 +615,14 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
file.transferTo(tempFile);
} catch (IOException e) {
return OcrResult.error("异常!" + e);
}
paramMap.put("file", tempFile);
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
......@@ -621,14 +643,21 @@ public class RetransmissionService {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
FileUtil.del(tempSaveDir);
OcrResult result = JSONUtil.toBean(res, OcrResult.class);
this.setRequestToRedis(2, startTime, endTime, result, ocrServerAddress, request);
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
addServerRequestCount(ocrServerAddress.getIp(), -1);
return result;
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
}
}
}
public void fileConvert(String fileId, MultipartFile srcFile, String dstFileType, HttpServletRequest request, HttpServletResponse response) {
......@@ -638,20 +667,15 @@ public class RetransmissionService {
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(srcFile.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
try {
srcFile.transferTo(tempFile);
} catch (IOException e) {
return;
}
paramMap.put("srcFile", srcFile);
try {
// ServerInfo ocrServerAddress = getOcrServerAddress();
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
returnError(response, 500, "OCR云平台未配置服务器信息");
paramMap.put("srcFile", tempFile);
if (null == ocrServerAddress) {
log.error("云平台未配置服务器信息");
return;
}
// setActiveServer(ocrServerAddress.getIp());
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/file/convert")
......@@ -661,22 +685,19 @@ public class RetransmissionService {
.contentType(ContentType.MULTIPART.getValue())
.form(paramMap).execute();
Map<String, List<String>> headers = httpResponse.headers();
if (!headers.isEmpty()) {
// 遍历赋值header
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
response.setHeader(entry.getKey(), entry.getValue().get(0));
}
System.out.println(httpResponse.body());
byte[] bytes = httpResponse.bodyBytes();
try (OutputStream outputStream = response.getOutputStream()) {
outputStream.write(bytes);
}
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
} finally {
FileUtil.del(tempSaveDir);
if (ObjUtil.isNotNull(ocrServerAddress)) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
// 赋值body
IoUtil.copy(httpResponse.bodyStream(), response.getOutputStream());
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
returnError(response, 500, "OCR云平台接口异常");
}
}
}
@Async("dataPersistThreadPoolTaskExecutor")
......
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