fix(OCR识别): 修改文件保存方式
This commit is contained in:
@@ -173,16 +173,12 @@
|
||||
<classifier>jdk17</classifier>
|
||||
<systemPath>${project.basedir}/lib/spirej.doc.cracked-11.4.2.jar</systemPath>
|
||||
</dependency>
|
||||
<!--<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml-schemas</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15on</artifactId>
|
||||
<version>1.56</version>
|
||||
<artifactId>bcprov-jdk14</artifactId>
|
||||
<version>1.64</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+53
-21
@@ -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<OcrRecordEO> {
|
||||
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<OcrRecordEO> {
|
||||
|
||||
@ApiOperation(value = "OCR识别-新增ocr内容", notes = "OCR识别-新增ocr内容")
|
||||
@PostMapping("/addOcrRecord")
|
||||
public ResponseMessage<OcrRecordEO> addOcrRecord(OcrRecordEO ocrRecordEO) throws Exception {
|
||||
public ResponseMessage<OcrRecordEO> 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<OcrRecordEO> {
|
||||
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<OcrRecordEO> {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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字段名转换为原始数据库列名。<b>如果不存在则返回null</b><br>
|
||||
* <p>字段列表:</p>
|
||||
|
||||
+4
-1
@@ -22,7 +22,7 @@
|
||||
<!-- OCR_RECORD table all fields -->
|
||||
<sql id="Base_Column_List" >
|
||||
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
|
||||
</sql>
|
||||
|
||||
<!-- 查询条件 -->
|
||||
@@ -181,6 +181,9 @@
|
||||
<if test="standName != null" >
|
||||
stand_name = #{standName},
|
||||
</if>
|
||||
<if test="attId != null" >
|
||||
att_id = #{attId},
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
+35
-29
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user