From b1e6943acf3c0c3945d8f82d77be7426e984569a Mon Sep 17 00:00:00 2001 From: yjz <3131811435@qq.com> Date: Wed, 5 Jun 2024 19:39:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(OCR=E8=AF=86=E5=88=AB):=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=96=87=E4=BB=B6=E4=BF=9D=E5=AD=98=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- laws-modules/pom.xml | 10 +-- .../ocr/controller/OCRRestfulController.java | 74 +++++++++++++------ .../modules/laws/ocr/entity/OcrRecordEO.java | 19 +++++ .../laws/ocr/mapper/xml/OcrRecordEOMapper.xml | 5 +- .../service/impl/OCRRestfulServiceImpl.java | 64 ++++++++-------- 5 files changed, 114 insertions(+), 58 deletions(-) diff --git a/laws-modules/pom.xml b/laws-modules/pom.xml index a4e94544..8a48c177 100644 --- a/laws-modules/pom.xml +++ b/laws-modules/pom.xml @@ -173,16 +173,12 @@ jdk17 ${project.basedir}/lib/spirej.doc.cracked-11.4.2.jar - org.bouncycastle - bcprov-jdk15on - 1.56 + bcprov-jdk14 + 1.64 compile + diff --git a/laws-modules/src/main/java/com/jero/modules/laws/ocr/controller/OCRRestfulController.java b/laws-modules/src/main/java/com/jero/modules/laws/ocr/controller/OCRRestfulController.java index 153c1352..d6b08075 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/ocr/controller/OCRRestfulController.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/ocr/controller/OCRRestfulController.java @@ -1,6 +1,7 @@ package com.jero.modules.laws.ocr.controller; import com.alibaba.fastjson.JSON; +import com.jero.common.util.MinioUtil; import com.jero.modules.laws.ocr.MD5Util; import com.jero.modules.laws.ocr.entity.OcrCallBackResultEo; import com.jero.modules.laws.ocr.entity.OcrRecordEO; @@ -11,8 +12,8 @@ import com.jero.modules.laws.ocr.page.OcrRecordEOPage; import com.jero.modules.laws.ocr.service.OCRRestfulService; import com.jero.modules.laws.ocr.service.OcrRecordEOService; import com.jero.modules.laws.ocr.web.BaseController; -import com.jero.modules.onlyoffice.entity.AttFileEO; -import com.jero.modules.onlyoffice.service.IAttFileEOService; +import com.jero.modules.oss.entity.OSSFile; +import com.jero.modules.oss.service.IOSSFileService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.StringUtils; @@ -26,12 +27,17 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; -import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Objects; /** * @program: OcrDemo @@ -49,8 +55,10 @@ public class OCRRestfulController extends BaseController { private OCRRestfulService ocrRestfulService; @Autowired private OcrRecordEOService ocrRecordEOService; +// @Autowired +// private IAttFileEOService attFileEOService; @Autowired - private IAttFileEOService attFileEOService; + private IOSSFileService ossFileService; //接口回调公钥 @Value("${OCR.publicKey}") private String OcrPublicKey; @@ -120,19 +128,16 @@ public class OCRRestfulController extends BaseController { @ApiOperation(value = "OCR识别-新增ocr内容", notes = "OCR识别-新增ocr内容") @PostMapping("/addOcrRecord") - public ResponseMessage addOcrRecord(OcrRecordEO ocrRecordEO) throws Exception { + public ResponseMessage addOcrRecord(@RequestBody OcrRecordEO ocrRecordEO) throws Exception { SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); logger.info("收到文件上传请求,开始处理文件:【"+sdf.format(new Date())+"】"); // 根据attID查找文件 String fileId = ocrRecordEO.getAttId(); - AttFileEO attFileEO = attFileEOService.getFileInfo(fileId); - if (attFileEO != null) { - String fileOriPath = filePath + attFileEO.getFilePath(); - String oriName = attFileEO.getOldFileName(); - File pdfFile = new File(fileOriPath+attFileEO.getFileName()); - FileInputStream fileInputStream = new FileInputStream(pdfFile); - MultipartFile multipartFile = new MockMultipartFile(oriName, oriName, - ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); + OSSFile ossFile = ossFileService.getById(fileId); + if (!Objects.isNull(ossFile)){ + InputStream minioFile = MinioUtil.getMinioFile(MinioUtil.getBucketName(), ossFile.getUrl()); + MultipartFile multipartFile = new MockMultipartFile(ossFile.getFileName(), ossFile.getFileName(), + ContentType.APPLICATION_OCTET_STREAM.toString(), minioFile); return ocrRestfulService.handleFile(multipartFile,multipartFile.getOriginalFilename(),ocrRecordEO,"add"); } else { return Result.error("0","无法找到该文件",null); @@ -150,14 +155,11 @@ public class OCRRestfulController extends BaseController { logger.info("收到文件上传请求,开始处理文件:【"+sdf.format(new Date())+"】"); // 根据attID查找文件 String fileId = ocrRecordEO.getAttId(); - AttFileEO attFileEO = attFileEOService.getFileInfo(fileId); - if (attFileEO != null) { - String fileOriPath = filePath + attFileEO.getFilePath(); - String oriName = attFileEO.getOldFileName(); - File pdfFile = new File(fileOriPath+attFileEO.getFileName()); - FileInputStream fileInputStream = new FileInputStream(pdfFile); - MultipartFile multipartFile = new MockMultipartFile(oriName, oriName, - ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); + OSSFile ossFile = ossFileService.getById(fileId); + if (!Objects.isNull(ossFile)){ + InputStream minioFile = MinioUtil.getMinioFile(MinioUtil.getBucketName(), ossFile.getUrl()); + MultipartFile multipartFile = new MockMultipartFile(ossFile.getFileName(), ossFile.getFileName(), + ContentType.APPLICATION_OCTET_STREAM.toString(), minioFile); return ocrRestfulService.handleFile(multipartFile,multipartFile.getOriginalFilename(),ocrRecordEO,"update"); } else { return Result.error("0","无法找到该文件",null); @@ -178,4 +180,34 @@ public class OCRRestfulController extends BaseController { } } + + @GetMapping("/demo") + public String demo() throws Exception { + File file = new File("C:\\Users\\31318\\Pictures\\Saved Pictures\\20200329222041_xxout.jpg"); + MultipartFile multipartFile = convertFileToMultipartFile(file); + assert multipartFile != null; + String originalFilename = multipartFile.getOriginalFilename(); + String upload = MinioUtil.upload(multipartFile, "/temp"); + return upload; + } + + public static MultipartFile convertFileToMultipartFile(File file) { + try { + Path path = Paths.get(file.getAbsolutePath()); + String name = file.getName(); + String originalFileName = file.getName(); + String contentType = Files.probeContentType(path); + byte[] content = null; + try { + content = Files.readAllBytes(path); + } catch (final IOException e) { + } + MultipartFile result = new MockMultipartFile(name, + originalFileName, contentType, content); + return result; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/ocr/entity/OcrRecordEO.java b/laws-modules/src/main/java/com/jero/modules/laws/ocr/entity/OcrRecordEO.java index 68bc404c..9cfad1b6 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/ocr/entity/OcrRecordEO.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/ocr/entity/OcrRecordEO.java @@ -38,6 +38,25 @@ public class OcrRecordEO extends BaseEntity implements Serializable{ private String standName; private String creationUser; private String attId; + private String wordAttId; + + public String getJsonAttId() { + return jsonAttId; + } + + public void setJsonAttId(String jsonAttId) { + this.jsonAttId = jsonAttId; + } + + public String getWordAttId() { + return wordAttId; + } + + public void setWordAttId(String wordAttId) { + this.wordAttId = wordAttId; + } + + private String jsonAttId; /** * java字段名转换为原始数据库列名。如果不存在则返回null
*

字段列表:

diff --git a/laws-modules/src/main/java/com/jero/modules/laws/ocr/mapper/xml/OcrRecordEOMapper.xml b/laws-modules/src/main/java/com/jero/modules/laws/ocr/mapper/xml/OcrRecordEOMapper.xml index d8b9339e..9fee9092 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/ocr/mapper/xml/OcrRecordEOMapper.xml +++ b/laws-modules/src/main/java/com/jero/modules/laws/ocr/mapper/xml/OcrRecordEOMapper.xml @@ -22,7 +22,7 @@ word_file_code, json_file_code, id, doc_real_name, json_real_name, file_name, doc_name, json_name, - creation_time, modify_time,result_content,file_type,stand_number,stand_name + creation_time, modify_time,result_content,file_type,stand_number,stand_name,att_id,word_att_id,json_att_id @@ -181,6 +181,9 @@ stand_name = #{standName}, + + att_id = #{attId}, + where id = #{id} diff --git a/laws-modules/src/main/java/com/jero/modules/laws/ocr/service/impl/OCRRestfulServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/ocr/service/impl/OCRRestfulServiceImpl.java index 96e421af..ab7b8a66 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/ocr/service/impl/OCRRestfulServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/ocr/service/impl/OCRRestfulServiceImpl.java @@ -1,17 +1,23 @@ package com.jero.modules.laws.ocr.service.impl; import com.alibaba.fastjson.JSONObject; +import com.jero.common.system.vo.LoginUser; +import com.jero.common.util.MinioUtil; +import com.jero.common.util.SnowflakeUtils; import com.jero.modules.laws.ocr.Base64Util; import com.jero.modules.laws.ocr.OkHttpUtil; import com.jero.modules.laws.ocr.RsaUtil; import com.jero.modules.laws.ocr.UUIDUtils; -import com.jero.modules.laws.ocr.mapper.OcrRecordEOMapper; import com.jero.modules.laws.ocr.entity.OcrRecordEO; import com.jero.modules.laws.ocr.entity.OcrResultEo; import com.jero.modules.laws.ocr.http.ResponseMessage; import com.jero.modules.laws.ocr.http.Result; +import com.jero.modules.laws.ocr.mapper.OcrRecordEOMapper; import com.jero.modules.laws.ocr.service.OCRRestfulService; +import com.jero.modules.oss.entity.OSSFile; +import com.jero.modules.oss.service.IOSSFileService; import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.SecurityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -52,6 +58,8 @@ public class OCRRestfulServiceImpl implements OCRRestfulService { @Autowired private OkHttpUtil okHttpUtil; @Autowired + private IOSSFileService ossFileService; + @Autowired private OcrRecordEOMapper ocrRecordEOMapper; //ocr处理文件后存放url @Value("${OCR.ocrPath}") @@ -103,13 +111,15 @@ public class OCRRestfulServiceImpl implements OCRRestfulService { if ("add".equals(type)) { OcrRecordEO ocrRecordEO = new OcrRecordEO(); ocrRecordEO.setId(taskId); - ocrRecordEO.setCreationUser("LoginUserUtil.getUserId()"); + LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + ocrRecordEO.setCreationUser(sysUser.getId()); ocrRecordEO.setFileName(fileName); ocrRecordEO.setCreationTime(new Date()); ocrRecordEO.setResultContent("转换中"); ocrRecordEO.setStandNumber(getOcrEO.getStandNumber()); ocrRecordEO.setStandName(getOcrEO.getStandName()); ocrRecordEO.setFileType(getOcrEO.getFileType()); + ocrRecordEO.setAttId(getOcrEO.getAttId()); ocrRecordEOMapper.insertSelective(ocrRecordEO); return Result.success(ocrResultEo.getResultCode(), "加入转换成功", taskId); } else { @@ -121,6 +131,7 @@ public class OCRRestfulServiceImpl implements OCRRestfulService { ocrRecordEO.setStandName(getOcrEO.getStandName()); ocrRecordEO.setFileType(getOcrEO.getFileType()); ocrRecordEO.setCreationTime(new Date()); + ocrRecordEO.setAttId(getOcrEO.getAttId()); ocrRecordEOMapper.updateByPrimaryKeySelective(ocrRecordEO); return Result.success(ocrResultEo.getResultCode(), "加入转换成功", taskId); } @@ -144,46 +155,41 @@ public class OCRRestfulServiceImpl implements OCRRestfulService { public String OcrHandleResult(MultipartFile wordFile,MultipartFile jsonFile,String taskId) throws Exception { //首先将文件保存至本地 - String saveWordFilePath=null; - String saveWordFileName=null; - logger.info("将文件保存至本地"); + String wordId=null; + String jsonId=null; + logger.info("保存文件"); if(wordFile!=null && !wordFile.isEmpty()){ - saveWordFileName=UUIDUtils.randomUUID20()+"_"+wordFile.getOriginalFilename().replace("/","_"); - saveWordFilePath=ocrFilePath+saveWordFileName; - logger.info(saveWordFilePath); - //生成保存文件 - File saveWordFile = new File(saveWordFilePath); - System.out.println(saveWordFile); - wordFile.transferTo(saveWordFile); -// FileUtils.copyInputStreamToFile(wordFile.getInputStream(),new File(saveWordFilePath)); -// savePic(wordFile.getInputStream(),saveWordFilePath); + String wordUrl = MinioUtil.upload(wordFile, "/temp"); + OSSFile ossFile = new OSSFile(); + ossFile.setId(String.valueOf(SnowflakeUtils.snowflake())); + ossFile.setFileName(wordFile.getOriginalFilename()); + ossFile.setUrl(wordUrl); + ossFileService.save(ossFile); + wordId = ossFile.getId(); logger.info("word存储完成"); } - String saveJsonFilePath=null; - String saveJsonFileName=null; if(jsonFile!=null && !jsonFile.isEmpty()){ - saveJsonFileName=UUIDUtils.randomUUID20()+"_"+jsonFile.getOriginalFilename().replace("/","_"); - saveJsonFilePath=ocrFilePath+saveJsonFileName; - logger.info(saveJsonFilePath); - File saveJsonFile = new File(saveJsonFilePath); - System.out.println(saveJsonFile); - jsonFile.transferTo(saveJsonFile); -// FileUtils.copyInputStreamToFile(jsonFile.getInputStream(),new File(saveJsonFilePath)); -// savePic(jsonFile.getInputStream(),saveJsonFilePath); + String jsonUrl = MinioUtil.upload(jsonFile, "/temp"); + OSSFile ossFile = new OSSFile(); + ossFile.setId(String.valueOf(SnowflakeUtils.snowflake())); + ossFile.setFileName(jsonFile.getOriginalFilename()); + ossFile.setUrl(jsonUrl); + ossFileService.save(ossFile); + jsonId = ossFile.getId(); logger.info("Json存储完成"); } //开始将文件保存至数据库中 - if(StringUtils.isNotEmpty(saveWordFilePath) && StringUtils.isNotEmpty(saveJsonFilePath)){ + if(StringUtils.isNotBlank(wordId) && StringUtils.isNotBlank(jsonId)){ OcrRecordEO ocrRecordEO = new OcrRecordEO(); ocrRecordEO.setId(taskId); - ocrRecordEO.setDocName(saveWordFilePath); - ocrRecordEO.setJsonName(saveJsonFilePath); - ocrRecordEO.setDocRealName(saveWordFileName); + ocrRecordEO.setDocRealName(wordFile.getOriginalFilename()); ocrRecordEO.setWordFileCode(null); - ocrRecordEO.setJsonRealName(saveJsonFileName); + ocrRecordEO.setJsonRealName(jsonFile.getOriginalFilename()); ocrRecordEO.setJsonFileCode(null); ocrRecordEO.setModifyTime(new Date()); ocrRecordEO.setResultContent("转换成功"); + ocrRecordEO.setWordAttId(wordId); + ocrRecordEO.setJsonAttId(jsonId); ocrRecordEOMapper.updateByPrimaryKeySelective(ocrRecordEO); return "{\"result\":\"success\"}"; }else{