ocr,拆分文件切换cos

This commit is contained in:
liyawei
2022-06-10 09:42:56 +08:00
parent 8214b38b07
commit 8185b9af62
8 changed files with 126 additions and 57 deletions
@@ -169,6 +169,63 @@ public class CosBootUtil {
} }
} }
/**
* cos 上传
* 相同key 文件会覆盖
* @param bytev
* @param key
*/
public static void uploadByte(byte[] bytev, String key){
init();
Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
// 生成预签名上传 URL
URL url = cosClient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.PUT);
log.info("生成预签名上传URL:" + url);
int responseCode = upLoadFileByte(url.toString(), bytev);
if (responseCode == 200) {
log.info("上传成功!");
} else {
log.info("上传失败!");
}
cosClient.shutdown();
}
private static int upLoadFileByte(String singnedurl,byte[] bytev){
InputStream inputStream= null;
DataOutputStream out = null;
int responseCode = 0;
try {
URL url= null;
url = new URL(singnedurl);
inputStream= new ByteArrayInputStream(bytev);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
out = new DataOutputStream(connection.getOutputStream());
// 写入要上传的数据
IOUtils.copy(inputStream, out);
responseCode = connection.getResponseCode();
log.info("Service returned response code " + responseCode);
} catch (ProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
try {
out.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return responseCode;
}
}
/** /**
* cos 下载 * cos 下载
* @param key * @param key
@@ -1,6 +1,7 @@
package com.jero.modules.oss.controller; package com.jero.modules.oss.controller;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.common.util.oss.CosBootUtil;
import com.jero.modules.oss.SplitFileUtils; import com.jero.modules.oss.SplitFileUtils;
import com.jero.modules.oss.service.IOSSFileService; import com.jero.modules.oss.service.IOSSFileService;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -14,11 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder; import sun.misc.BASE64Decoder;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64; import java.util.Base64;
@Slf4j @Slf4j
@@ -52,13 +49,15 @@ public class SplitFileController {
"attachment; filename=\""+ SplitFileUtils.encodeFileName(fileName, request) +"\""); "attachment; filename=\""+ SplitFileUtils.encodeFileName(fileName, request) +"\"");
response.setContentType("application/force-download"); response.setContentType("application/force-download");
try(OutputStream os = response.getOutputStream(); try(OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(fileUrl);) { /*FileInputStream fis = new FileInputStream(fileUrl);*/) {
if (CosBootUtil.doesObjectExist(fileUrl)) {
int len = 0; FileInputStream fis = (FileInputStream) CosBootUtil.download(fileUrl);
while ((len = fis.read()) != -1) { int len = 0;
os.write(len); while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
} }
os.flush();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new JeroBootException("文件不存在"); throw new JeroBootException("文件不存在");
} catch (IOException e) { } catch (IOException e) {
@@ -19,9 +19,11 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@@ -42,8 +44,8 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
private String[] fileSuffixLimits; private String[] fileSuffixLimits;
// 文档拆分条款图片访问路径 // 文档拆分条款图片访问路径
@Value(value = "${jero.path.img}") @Value(value = "${jero.splitUrl}")
private String imgpath; private String splitUrl;
@Override @Override
public void upload(MultipartFile multipartFile) throws IOException { public void upload(MultipartFile multipartFile) throws IOException {
@@ -290,10 +292,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
String ctxPath = uploadCospath; String ctxPath = uploadCospath;
String fileName = null; String fileName = null;
String fileType = null; String fileType = null;
File file = new File(ctxPath + File.separator + bizPath + File.separator);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String orgName = mf.getOriginalFilename();// 获取文件名 String orgName = mf.getOriginalFilename();// 获取文件名
orgName = CommonUtils.getFileName(orgName); orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(".") != -1) { if (orgName.indexOf(".") != -1) {
@@ -302,6 +301,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
fileName = orgName + "_" + System.currentTimeMillis(); fileName = orgName + "_" + System.currentTimeMillis();
} }
String filePath = ctxPath + "/" + fileName;
fileType = orgName.substring(orgName.lastIndexOf(".")); fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型 //判断文件类型
@@ -344,32 +344,24 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
} }
} }
String savePath = file.getPath() + File.separator + fileName; // 将文件上传至腾讯云 cos
File savefile = new File(savePath); CosBootUtil.upload(mf, filePath);
FileCopyUtils.copy(mf.getBytes(), savefile);
String dbpath = null;
if (oConvertUtils.isNotEmpty(bizPath)) {
dbpath = bizPath + File.separator + fileName;
} else {
dbpath = fileName;
}
if (dbpath.contains("\\")) {
dbpath = dbpath.replace("\\", "/");
}
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//存入文件表 //存入文件表
oSSFile.setFileName(orgName); oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID()); oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(savePath); oSSFile.setUrl(filePath);
oSSFile.setCreateBy(loginUser.getUsername()); oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date()); oSSFile.setCreateTime(new Date());
this.save(oSSFile); this.save(oSSFile);
// 文件路径做特殊处理 // 文件路径做特殊处理
String imgUrl = imgpath + savePath.substring(savePath.lastIndexOf(File.separator)+1); BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
oSSFile.setUrl(imgUrl); oSSFile.setUrl(imgUrl);
return oSSFile; return oSSFile;
} catch (IOException e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
return oSSFile; return oSSFile;
@@ -6,6 +6,7 @@ import com.jero.common.api.vo.Result;
import com.jero.common.constant.enums.CutEnum; import com.jero.common.constant.enums.CutEnum;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser; import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.oss.CosBootUtil;
import com.jero.modules.ocr.entity.OcrRecordEO; import com.jero.modules.ocr.entity.OcrRecordEO;
import com.jero.modules.ocr.enums.ResultContentEnum; import com.jero.modules.ocr.enums.ResultContentEnum;
import com.jero.modules.ocr.service.IOcrRecordEOService; import com.jero.modules.ocr.service.IOcrRecordEOService;
@@ -88,7 +89,10 @@ public class OcrRestfulController{
response.setContentType("application/octet-stream"); response.setContentType("application/octet-stream");
String fullPath = ocrPath + fileName; String fullPath = ocrPath + fileName;
is = new FileInputStream(fullPath); // is = new FileInputStream(fullPath);
if (CosBootUtil.doesObjectExist(fullPath)) {
is = CosBootUtil.download(fullPath);
}
os = response.getOutputStream(); os = response.getOutputStream();
IOUtils.copy(is, os); IOUtils.copy(is, os);
os.flush(); os.flush();
@@ -3,6 +3,7 @@ package com.jero.modules.ocr.service.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jero.common.api.vo.Result; import com.jero.common.api.vo.Result;
import com.jero.common.constant.enums.CutEnum; import com.jero.common.constant.enums.CutEnum;
import com.jero.common.util.oss.CosBootUtil;
import com.jero.modules.FeignClient.LocalToolFeignClient; import com.jero.modules.FeignClient.LocalToolFeignClient;
import com.jero.modules.ocr.entity.OcrRecordEO; import com.jero.modules.ocr.entity.OcrRecordEO;
import com.jero.modules.ocr.entity.OcrResultEO; import com.jero.modules.ocr.entity.OcrResultEO;
@@ -227,15 +228,17 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
String saveWordFileName=null; String saveWordFileName=null;
if(wordFile!=null && !wordFile.isEmpty()){ if(wordFile!=null && !wordFile.isEmpty()){
saveWordFileName= UUIDUtils.randomUUID20() + "_"+wordFile.getOriginalFilename(); saveWordFileName= UUIDUtils.randomUUID20() + "_"+wordFile.getOriginalFilename();
saveWordFilePath=ocrFilePath+saveWordFileName; saveWordFilePath= ocrFilePath + saveWordFileName;
FileUtils.copyInputStreamToFile(wordFile.getInputStream(),new File(saveWordFilePath)); // FileUtils.copyInputStreamToFile(wordFile.getInputStream(),new File(saveWordFilePath));
CosBootUtil.upload(wordFile, saveWordFilePath);
} }
String saveJsonFilePath=null; String saveJsonFilePath=null;
String saveJsonFileName=null; String saveJsonFileName=null;
if(jsonFile!=null && !jsonFile.isEmpty()){ if(jsonFile!=null && !jsonFile.isEmpty()){
saveJsonFileName=UUIDUtils.randomUUID20() + "_"+jsonFile.getOriginalFilename(); saveJsonFileName=UUIDUtils.randomUUID20() + "_"+jsonFile.getOriginalFilename();
saveJsonFilePath=ocrFilePath+saveJsonFileName; saveJsonFilePath= ocrFilePath + saveJsonFileName;
FileUtils.copyInputStreamToFile(jsonFile.getInputStream(),new File(saveJsonFilePath)); // FileUtils.copyInputStreamToFile(jsonFile.getInputStream(),new File(saveJsonFilePath));
CosBootUtil.upload(jsonFile, saveJsonFilePath);
} }
//开始将文件保存至数据库中 //开始将文件保存至数据库中
if(StringUtils.isNotEmpty(saveWordFilePath) && StringUtils.isNotEmpty(saveJsonFilePath)){ if(StringUtils.isNotEmpty(saveWordFilePath) && StringUtils.isNotEmpty(saveJsonFilePath)){
@@ -311,12 +311,13 @@ public class FileSplitItemsEOController extends JeroController<SarFileSplitItems
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象 MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
if (oConvertUtils.isEmpty(bizPath)) { if (oConvertUtils.isEmpty(bizPath)) {
if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) { bizPath = "";
//未指定目录,则用阿里云默认目录 upload // if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) {
bizPath = "upload"; // //未指定目录,则用阿里云默认目录 upload
} else { // bizPath = "upload";
bizPath = ""; // } else {
} // bizPath = "";
// }
} }
OSSFile oSSFile = ossFileService.uploadLocalForSplit(file, bizPath,state,cut); OSSFile oSSFile = ossFileService.uploadLocalForSplit(file, bizPath,state,cut);
if (oConvertUtils.isNotEmpty(oSSFile)) { if (oConvertUtils.isNotEmpty(oSSFile)) {
@@ -3,6 +3,7 @@ package com.jero.modules.split.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser; import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.oss.CosBootUtil;
import com.jero.modules.document.utils.ReadPdfUtil; import com.jero.modules.document.utils.ReadPdfUtil;
import com.jero.modules.ocr.util.UUIDUtils; import com.jero.modules.ocr.util.UUIDUtils;
import com.jero.modules.oss.entity.OSSFile; import com.jero.modules.oss.entity.OSSFile;
@@ -15,8 +16,10 @@ import com.jero.modules.split.service.IFileSplitItemsEOService;
import com.jero.modules.split.service.ISarFileSplitItemsValEOService; import com.jero.modules.split.service.ISarFileSplitItemsValEOService;
import com.jero.modules.split.util.FileSplitUtils; import com.jero.modules.split.util.FileSplitUtils;
import com.jero.modules.split.util.ReadWordTable; import com.jero.modules.split.util.ReadWordTable;
import com.qcloud.cos.utils.IOUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.pdfbox.cos.COSBase;
import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.*;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlCursor;
@@ -65,6 +68,8 @@ public class FileSpiltService {
@Value("${jero.path.upload}") @Value("${jero.path.upload}")
private String filePath; private String filePath;
@Value("${jero.path.uploadCos}")
private String filePathCos;
@Value("${jero.splitUrl}") @Value("${jero.splitUrl}")
private String splitUrl; private String splitUrl;
@@ -132,14 +137,16 @@ public class FileSpiltService {
OSSFile ossFile = ossFileList.get(0); OSSFile ossFile = ossFileList.get(0);
String readFilePath = ossFile.getUrl(); String readFilePath = ossFile.getUrl();
if (StringUtils.isEmpty(readFilePath)) { if (StringUtils.isEmpty(readFilePath) || !CosBootUtil.doesObjectExist(readFilePath)) {
throw new JeroBootException("当前文件未找到,请重新选择!"); throw new JeroBootException("当前文件未找到,请重新选择!");
} }
// AttFileEO attFileEO = attFileEOService.getFileInfo(sarFileSplitInfoEO.getAttId()); // AttFileEO attFileEO = attFileEOService.getFileInfo(sarFileSplitInfoEO.getAttId());
// String readFilePath = filePath + "/" + attFileEO.getFilePath() + attFileEO.getFileName(); // String readFilePath = filePath + "/" + attFileEO.getFilePath() + attFileEO.getFileName();
// String readFilePath = "C:\\Users\\wangc\\Desktop\\蔚来演示数据\\GB 11551-2014 汽车正面碰撞的乘员保护.docx"; // String readFilePath = "C:\\Users\\wangc\\Desktop\\蔚来演示数据\\GB 11551-2014 汽车正面碰撞的乘员保护.docx";
InputStream is = new FileInputStream(readFilePath); //需要将文件路更改为word文档所在路径。 // InputStream is = new FileInputStream(readFilePath); //需要将文件路更改为word文档所在路径。
InputStream is = CosBootUtil.download(readFilePath); //需要将文件路更改为word文档所在路径。
XWPFDocument doc = new XWPFDocument(is); XWPFDocument doc = new XWPFDocument(is);
Pattern ptest = Pattern.compile("^1范围"); Pattern ptest = Pattern.compile("^1范围");
@@ -431,7 +438,8 @@ public class FileSpiltService {
} }
OSSFile ossFile = ossFileList.get(0); OSSFile ossFile = ossFileList.get(0);
String readFilePath = ossFile.getUrl(); String readFilePath = ossFile.getUrl();
if (StringUtils.isEmpty(readFilePath)) {
if (StringUtils.isEmpty(readFilePath) || !CosBootUtil.doesObjectExist(readFilePath)) {
throw new JeroBootException("当前文件未找到,请重新选择!"); throw new JeroBootException("当前文件未找到,请重新选择!");
} }
@@ -443,7 +451,14 @@ public class FileSpiltService {
throw new JeroBootException("GSO必须选择PDF文件!"); throw new JeroBootException("GSO必须选择PDF文件!");
} }
String readPdf = ReadPdfUtil.readPdf(readFilePath); InputStream in = CosBootUtil.download(readFilePath);
// 从 cos 拿流写入本地文件
String pdfFilePath = filePath + readFilePath.substring(readFilePath.lastIndexOf("/"));
File file = new File(pdfFilePath);
OutputStream out = new FileOutputStream(file);
IOUtils.copy(in, out);
String readPdf = ReadPdfUtil.readPdf(pdfFilePath);
if (StringUtils.isEmpty(readPdf)) { if (StringUtils.isEmpty(readPdf)) {
throw new JeroBootException("未读取到文件内容,请检查后重试!"); throw new JeroBootException("未读取到文件内容,请检查后重试!");
} }
@@ -577,6 +592,9 @@ public class FileSpiltService {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return -1; return -1;
} finally {
// 删除创建的临时文件 TODO
} }
if (treeList.size() > 0 && messageList.size() > 0) { if (treeList.size() > 0 && messageList.size() > 0) {
sarFileSplitMenuEOMapper.insertForeach(treeList); sarFileSplitMenuEOMapper.insertForeach(treeList);
@@ -2557,19 +2575,14 @@ public class FileSpiltService {
XWPFPictureData pictureData = p.getDocument().getPictureDataByID(pictureId); XWPFPictureData pictureData = p.getDocument().getPictureDataByID(pictureId);
String imageName = UUIDUtils.randomUUID10() + pictureData.getFileName(); String imageName = UUIDUtils.randomUUID10() + pictureData.getFileName();
byte[] bytev = pictureData.getData(); byte[] bytev = pictureData.getData();
String outPath = filePath + File.separator; String filepathandname = filePathCos + "/" + imageName;
File file1 = new File(outPath);
if (!file1.exists()) {
file1.mkdirs();
}
String filepathandname = filePath + File.separator + imageName;
try { try {
FileOutputStream fos = new FileOutputStream(filepathandname);
fos.write(bytev);
// String fileUrl = splitUrl + "/" + filepathandname; // String fileUrl = splitUrl + "/" + filepathandname;
BASE64Encoder base64Encoder = new BASE64Encoder(); BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(filepathandname.getBytes(StandardCharsets.UTF_8)); String encode = base64Encoder.encode(filepathandname.getBytes(StandardCharsets.UTF_8));
String path = splitUrl+"/jero-boot/sys/split/file/getImage?fileUrl="+encode; String path = splitUrl+"/jero-boot/sys/split/file/getImage?fileUrl="+encode;
CosBootUtil.uploadByte(bytev, encode);
String imgCon = path; String imgCon = path;
String imgConVal = "<img class=\'wordImg\' src=\'" + path + "\'>"; String imgConVal = "<img class=\'wordImg\' src=\'" + path + "\'>";
message.getItemsCondi().add(imgConVal); message.getItemsCondi().add(imgConVal);
@@ -197,7 +197,7 @@ jero :
#文件上传根目录 设置 #文件上传根目录 设置
uploadCos: /upFiles uploadCos: /upFiles
upload: D://opt//upFiles upload: D://opt//upFiles
img: D://opt//upFiles/ img: /upFiles/
#webapp文件路径 #webapp文件路径
webapp: D://opt//webapp webapp: D://opt//webapp
#导出pdf临时文件路径 设置 #导出pdf临时文件路径 设置
@@ -336,7 +336,7 @@ OCR:
# OCR接口公钥 # OCR接口公钥
publicKey: EC4KKA6ZDTCPAOCRBC5M publicKey: EC4KKA6ZDTCPAOCRBC5M
# OCR文件存储路径 # OCR文件存储路径
ocrPath: D:/APPSOFT/LAWSOCRDEMO/OCRFILE/ ocrPath: /ocrFiles/
#ocrPath: /data/DeploymentPackage/laws-shanqi/APPSOFT/LAWSOCRDEMO/OCRFILE/ #ocrPath: /data/DeploymentPackage/laws-shanqi/APPSOFT/LAWSOCRDEMO/OCRFILE/
# OCR文件下载地址(onlyoffice专用) # OCR文件下载地址(onlyoffice专用)
ocrDownPath: http://10.0.1.25:8080/jero-boot/ocr/ocrCheck/downFile ocrDownPath: http://10.0.1.25:8080/jero-boot/ocr/ocrCheck/downFile