ocr识别-回调修改

This commit is contained in:
liyawei
2022-03-08 11:34:57 +08:00
parent 1936890016
commit 936edb747b
8 changed files with 124 additions and 94 deletions
@@ -71,6 +71,7 @@ public class ShiroConfig {
}
// 配置不会被拦截的链接 顺序判断
filterChainDefinitionMap.put("/sys/cas/client/validateLogin", "anon"); //cas验证登录
filterChainDefinitionMap.put("/ocr/OcrRestful/ocrHandleResult", "anon"); //ocr回调接口
filterChainDefinitionMap.put("/sys/randomImage/**", "anon"); //登录验证码接口排除
filterChainDefinitionMap.put("/sys/checkCaptcha", "anon"); //登录验证码接口排除
filterChainDefinitionMap.put("/sys/getRSAPublicKey", "anon"); //获取RSA公钥接口排除
@@ -1,6 +1,8 @@
package com.jero.modules.system.vo;
import lombok.Data;
import java.io.Serializable;
/**
@@ -9,6 +11,7 @@ import java.io.Serializable;
* <b>日期:</b> 2019-03-25 <br>
* <b>版权所有:<b>版权归北京卡达克数据技术中心所有。<br>
*/
@Data
public class OcrVO implements Serializable{
private String restHandleFileUrl;
private String OcrUserId;
@@ -18,68 +21,5 @@ public class OcrVO implements Serializable{
private String fileContent;
private String taskId;
private String RestCallBackUrl;
public String getRestHandleFileUrl() {
return restHandleFileUrl;
}
public void setRestHandleFileUrl(String restHandleFileUrl) {
this.restHandleFileUrl = restHandleFileUrl;
}
public String getOcrUserId() {
return OcrUserId;
}
public void setOcrUserId(String ocrUserId) {
OcrUserId = ocrUserId;
}
public String getAuthCode() {
return authCode;
}
public void setAuthCode(String authCode) {
this.authCode = authCode;
}
public String getConvertType() {
return convertType;
}
public void setConvertType(String convertType) {
this.convertType = convertType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileContent() {
return fileContent;
}
public void setFileContent(String fileContent) {
this.fileContent = fileContent;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public String getRestCallBackUrl() {
return RestCallBackUrl;
}
public void setRestCallBackUrl(String restCallBackUrl) {
RestCallBackUrl = restCallBackUrl;
}
private Boolean isThird; //调用方:单纯获取识别的返回结果
}
@@ -1,11 +1,9 @@
package com.jero.modules.ocr.controller;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.jero.common.api.vo.Result;
import com.jero.modules.ocr.entity.OcrCallBackResultEO;
import com.jero.modules.ocr.entity.OcrRecordEO;
import com.jero.modules.ocr.enums.ResultContentEnum;
import com.jero.modules.ocr.service.IOcrRecordEOService;
import com.jero.modules.ocr.service.IOcrRestfulService;
import com.jero.modules.ocr.util.MD5Util;
@@ -26,7 +24,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @program: OcrDemo
@@ -81,30 +78,32 @@ public class OcrRestfulController{
* @return
*/
@ApiOperation(value = "OCR回调")
@PostMapping("/OcrHandleResult")
public String OcrHandleResult(@RequestParam("wordFile") MultipartFile wordFile,@RequestParam("jsonFile") MultipartFile jsonFile, OcrCallBackResultEO ocrCallBackResultEo) {
@PostMapping("/ocrHandleResult")
public String ocrHandleResult(@RequestParam("wordFile") MultipartFile wordFile,
@RequestParam("jsonFile") MultipartFile jsonFile,
@RequestParam("taskId") String taskId,
@RequestParam("result") String result,
@RequestParam("key") String key) {
try{
log.info("调取到我了");
log.info("接口回调结果:"+ JSON.toJSONString(ocrCallBackResultEo));
log.info("接口回调时所传文件---wordFile:"+ wordFile.toString() +"; jsonFile:"+jsonFile.toString());
if("error".equals(ocrCallBackResultEo.getResult())){
if("error".equals(result)){
log.info("客户接口处理文件失败");
ocrRestfulService.updateDb(ocrCallBackResultEo.getTaskID(),"客户接口处理文件失败");
ocrRestfulService.updateDb(taskId,"客户接口处理文件失败");
return "{\"result\":\"error\"}";
}
//1.验证 key 是否符合,不符合打回,符合继续
//2.向后传递文件
String key = ocrCallBackResultEo.getKey();
String sign = ocrCallBackResultEo.getTaskID() + OcrPublicKey;
String sign = taskId + OcrPublicKey;
String signMD5 = MD5Util.string2MD5(sign);
// String encpySign =MD5Util.convertMD5(signMD5);
if(!signMD5.equals(key)){
//不同,则认证失败
log.info("认证失败");
ocrRestfulService.updateDb(ocrCallBackResultEo.getTaskID(),"回调接口认证失败");
ocrRestfulService.updateDb(taskId, ResultContentEnum.OCR_CONVERT_FAIL.getValue());
return "{\"result\":\"Authentication failed\"}";
}
return ocrRestfulService.OcrHandleResult(wordFile,jsonFile,ocrCallBackResultEo.getTaskID());
return ocrRestfulService.ocrHandleResult(wordFile,jsonFile,taskId);
}catch (Exception e){
log.error(e.getMessage(),e);
return "{\"result\":\"runTimeException\"}";
@@ -1,6 +1,5 @@
package com.jero.modules.ocr.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jero.common.api.vo.Result;
import com.jero.modules.ocr.entity.OcrRecordEO;
import org.springframework.web.multipart.MultipartFile;
@@ -13,7 +12,7 @@ import org.springframework.web.multipart.MultipartFile;
public interface IOcrRestfulService{
Result<?> handleFile(MultipartFile file, String fileName, OcrRecordEO getOcrEO, String type) throws Exception;
String OcrHandleResult(MultipartFile wordFile,MultipartFile jsonFile,String taskId) throws Exception;
String ocrHandleResult(MultipartFile wordFile,MultipartFile jsonFile,String taskId) throws Exception;
OcrRecordEO getOcrResult(String taskId, int i) throws Exception;
@@ -10,6 +10,7 @@ import com.jero.modules.ocr.service.IOcrRecordEOService;
import com.jero.modules.ocr.service.IOcrRestfulService;
import com.jero.modules.ocr.util.Base64Util;
import com.jero.modules.ocr.util.RsaUtil;
import com.jero.modules.ocr.util.UUIDUtils;
import com.jero.modules.system.vo.OcrVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
@@ -31,7 +32,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @program: OcrDemo
@@ -86,7 +86,7 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
public Result<?> handleFile(MultipartFile file, String fileName, OcrRecordEO getOcrEO, String type) throws Exception {
String taskId = "";
if ("add".equals(type)) {
taskId = UUID.randomUUID().toString().replace("-", "");
taskId = UUIDUtils.randomUUID20();
} else {
taskId = getOcrEO.getId();
}
@@ -126,6 +126,7 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
ocrVO.setFileContent(fileContent);
ocrVO.setTaskId(taskId);
ocrVO.setRestCallBackUrl(RestCallBackUrl);
ocrVO.setIsThird(true);
ResponseEntity<String> responseEntity = localToolFeignClient.getFile(ocrVO);
log.info("请求OCR接口完毕,返回响应状态:【"+sdf.format(new Date())+"");
@@ -173,19 +174,19 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
}
}
public String OcrHandleResult(MultipartFile wordFile,MultipartFile jsonFile,String taskId) throws Exception {
public String ocrHandleResult(MultipartFile wordFile,MultipartFile jsonFile,String taskId) throws Exception {
//首先将文件保存至本地
String saveWordFilePath=null;
String saveWordFileName=null;
if(wordFile!=null && !wordFile.isEmpty()){
saveWordFileName= UUID.randomUUID().toString().replace("-", "") + "_"+wordFile.getOriginalFilename();
saveWordFileName= UUIDUtils.randomUUID20() + "_"+wordFile.getOriginalFilename();
saveWordFilePath=ocrFilePath+saveWordFileName;
FileUtils.copyInputStreamToFile(wordFile.getInputStream(),new File(saveWordFilePath));
}
String saveJsonFilePath=null;
String saveJsonFileName=null;
if(jsonFile!=null && !jsonFile.isEmpty()){
saveJsonFileName=UUID.randomUUID().toString().replace("-", "") + "_"+jsonFile.getOriginalFilename();
saveJsonFileName=UUIDUtils.randomUUID20() + "_"+jsonFile.getOriginalFilename();
saveJsonFilePath=ocrFilePath+saveJsonFileName;
FileUtils.copyInputStreamToFile(jsonFile.getInputStream(),new File(saveJsonFilePath));
}
@@ -236,7 +237,7 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
ocrRecordEO.setId(taskId);
ocrRecordEO.setUpdateTime(new Date());
ocrRecordEO.setResultContent(result);
ocrRecordEOService.editById(ocrRecordEO);
ocrRecordEOService.updateById(ocrRecordEO);
}
public boolean checkFileName(String fileName){
@@ -0,0 +1,57 @@
package com.jero.modules.ocr.util;
import java.awt.*;
import java.util.Random;
/**
* @Author: liyawei
* @Description:
* @Date: Created in 0:09 2022/3/8
*/
public class RandomUtils extends org.apache.commons.lang3.RandomUtils {
private static final char[] codeSeq = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'};
private static final char[] numberArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
private static Random random = new Random();
public RandomUtils() {
}
public static final String randomString(int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; ++i) {
sb.append(String.valueOf(codeSeq[random.nextInt(codeSeq.length)]));
}
return sb.toString();
}
public static final String randomNumberString(int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; ++i) {
sb.append(String.valueOf(numberArray[random.nextInt(numberArray.length)]));
}
return sb.toString();
}
public static Color randomColor(int fc, int bc) {
int f = fc;
int b = bc;
Random random = new Random();
if (fc > 255) {
f = 255;
}
if (bc > 255) {
b = 255;
}
return new Color(f + random.nextInt(b - f), f + random.nextInt(b - f), f + random.nextInt(b - f));
}
public static int nextInt(int bound) {
return random.nextInt(bound);
}
}
@@ -0,0 +1,38 @@
package com.jero.modules.ocr.util;
import java.util.Random;
public class UUIDUtils {
public static String randomUUID10() {
return RandomUtils.randomString(10);
}
public static String randomUUID20() {
return RandomUtils.randomString(20);
}
public static String randomUUID(int length) {
return RandomUtils.randomString(length);
}
public static String getUUIDPath(String uuid){
StringBuilder builder=new StringBuilder();
builder.append("/");
builder.append((uuid.substring(0, 3).hashCode())%100+"").append("/");
builder.append((uuid.substring(7,10).hashCode())%100+"").append("/");
builder.append((uuid.substring(11,14).hashCode())%100+"").append("/");
return builder.toString();
}
public static String getAttTable(){
Random rand = new Random();
int nextInt = rand.nextInt(10)+1;
StringBuilder builder=new StringBuilder();
builder.append("ATT_FILE_").append(String.format("%02d", nextInt));
return builder.toString();
}
}
@@ -299,11 +299,11 @@ justauth:
# 云端OCR识别集成配置参数
OCR:
#请求OCR转换接口
handleFileUrl: https://sws.trans-cosmos.com.cn/WebService.asmx/FileConversion
#handleFileUrl = https://61.136.1.103:8091/WebService.asmx/FileConversion
# handleFileUrl: https://sws.trans-cosmos.com.cn/WebService.asmx/FileConversion
handleFileUrl: http://61.136.1.103:8091/WebService.asmx/FileConversion
# OCR回调接口地址 配置客户本地的IP及端口号
# callBackUrl: http://139.9.235.66:9110/api/ocr/OCRRestful/OcrHandleResult
callBackUrl: http://127.0.0.1:8080/api/ocr/OCRRestful/OcrHandleResult
# callBackUrl: http://139.9.235.66:9110//api/ocr/OCRRestful/OcrHandleResult
callBackUrl: http://139.9.235.66:9022/local-tool/ocr/ocrHandleResult
# OCR认证用户
userId: dayuzhou1234
# OCR认证编码
@@ -320,11 +320,6 @@ OCR:
times: 20
# OCR转换类型
convertType: BOTH
## 这个IFeignService是加了@FeignClient注解的类
local-tool:
ribbon:
## 服务提供者的地址,不是服务注册中心的地址
listOfServers: http://139.9.235.66:9022
## 这个要有,如果不加,只加了上面也没用
ribbon: