ocr,拆分文件切换cos

This commit is contained in:
liyawei
2022-06-10 12:17:29 +08:00
parent 7b8707bec5
commit b8e0ee3a11
5 changed files with 67 additions and 37 deletions
@@ -51,7 +51,7 @@ public class SplitFileController {
try(OutputStream os = response.getOutputStream();
/*FileInputStream fis = new FileInputStream(fileUrl);*/) {
if (CosBootUtil.doesObjectExist(fileUrl)) {
FileInputStream fis = (FileInputStream) CosBootUtil.download(fileUrl);
InputStream fis = CosBootUtil.download(fileUrl);
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
@@ -187,9 +187,16 @@ public class OcrRestfulController{
if (ObjectUtil.isNotEmpty(ossFile)) {
String fileOriPath = ossFile.getUrl();
String oriName = ossFile.getFileName();
File pdfFile = new File(fileOriPath);
try {
FileInputStream fileInputStream = new FileInputStream(pdfFile);
if (!CosBootUtil.doesObjectExist(fileOriPath)) {
if (CutEnum.CN.getValue().equals(ocrRecordEO.getCut())) {
return Result.error("无法找到该文件");
} else {
return Result.error("File not found");
}
}
InputStream fileInputStream = CosBootUtil.download(fileOriPath);
MultipartFile multipartFile = new MockMultipartFile(oriName, oriName,
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
return ocrRestfulService.handleFile(multipartFile,multipartFile.getOriginalFilename(),ocrRecordEO,"add");
@@ -459,6 +459,7 @@ public class FileSpiltService {
OutputStream out = new FileOutputStream(file);
IOUtils.copy(in, out);
String readPdf = ReadPdfUtil.readPdf(pdfFilePath);
file.delete();
if (StringUtils.isEmpty(readPdf)) {
throw new JeroBootException("未读取到文件内容,请检查后重试!");
}
@@ -592,9 +593,6 @@ public class FileSpiltService {
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
// 删除创建的临时文件 TODO
}
if (treeList.size() > 0 && messageList.size() > 0) {
sarFileSplitMenuEOMapper.insertForeach(treeList);
@@ -19,6 +19,7 @@ import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.DateUtils;
import com.jero.common.util.oss.CosBootUtil;
import com.jero.generater.modules.online.cgform.entity.OnlCgformField;
import com.jero.generater.modules.online.cgform.service.impl.OnlCgformFieldServiceImpl;
import com.jero.modules.document.enums.FieldTypeEnum;
@@ -77,10 +78,13 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
@@ -129,8 +133,8 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
@Value(value = "${jero.path.upload}")
private String uploadpath;
@Value(value = "${jero.path.img}")
private String imgpath;
@Value(value = "${jero.splitUrl}")
private String splitUrl;
@Override
public int addItemContent(Map<String, Object> parameter){
@@ -157,10 +161,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
if ("TEXT".equals(valEO.getType())) {
valContent = "<p>" + valEO.getItemContent() + "</p>";
} else if ("IMG".equals(valEO.getType())) {
String ossFileId = valEO.getItemContent();
OSSFile ossFile = ossFileService.getById(ossFileId);
String url = ossFile.getUrl();
String imgUrl = imgpath + url.substring(url.lastIndexOf(File.separator)+1);
String imgUrl = valEO.getItemContent();
valEO.setItemContent(imgUrl);
valContent = "<img class=\"wordImg\" src=\""+ valEO.getItemContent() +"\">";
// valContent = valEO.getItemContent();
@@ -235,13 +236,8 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
if ("TEXT".equals(valEO.getType())) {
valContent = "<p>" + valEO.getItemContent() + "</p>";
} else if ("IMG".equals(valEO.getType())) {
if(!valEO.getItemContent().contains("<img")) {
String ossFileId = valEO.getItemContent();
OSSFile ossFile = ossFileService.getById(ossFileId);
String url = ossFile.getUrl();
String imgUrl = imgpath + url.substring(url.lastIndexOf(File.separator)+1);
valEO.setItemContent(imgUrl);
}
String imgUrl = valEO.getItemContent();
valEO.setItemContent(imgUrl);
valContent = "<img class=\"wordImg\" src=\"" + valEO.getItemContent() + "\">";
// valContent = valEO.getItemContent();
} else if ("TABLE".equals(valEO.getType())) {
@@ -1221,11 +1217,15 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
// if (fileTypeStr.contains(fileSuffix)) {
// state = "1";
// }
OSSFile oSSFile = ossFileService.uploadLocal(multipartFile, "",state,null);
OSSFile oSSFile = ossFileService.uploadLocalOfCos(multipartFile, "",state,null);
if(oSSFile!=null){
oSSFile = ossFileService.getById(oSSFile.getId());
isText = false;
String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
// valEO.setItemContent(imgCon);
@@ -1266,12 +1266,16 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
// if (fileTypeStr.contains(fileSuffix)) {
// state = "1";
// }
OSSFile oSSFile = ossFileService.uploadLocal(multipartFile, "", state,null);
OSSFile oSSFile = ossFileService.uploadLocalOfCos(multipartFile, "", state,null);
oSSFile = ossFileService.getById(oSSFile.getId());
if (oSSFile != null) {
oSSFile = ossFileService.getById(oSSFile.getId());
isText = false;
String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
// valEO.setItemContent(imgCon);
@@ -1310,11 +1314,15 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
String url = nowfilelist.get(0).getPath();
MultipartFile multipartFile = createMfileByPath(url);
String state = "0";
OSSFile oSSFile = ossFileService.uploadLocal(multipartFile, "", state,null);
OSSFile oSSFile = ossFileService.uploadLocalOfCos(multipartFile, "", state,null);
oSSFile = ossFileService.getById(oSSFile.getId());
if (oSSFile != null) {
isText = false;
String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(oSSFile.getUrl().getBytes(StandardCharsets.UTF_8));
String path = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
// String path = imgpath + oSSFile.getUrl().substring(oSSFile.getUrl().lastIndexOf(File.separator)+1);
String imgCon = "<img class=\"wordImg\" src=\"" + path + "\">";
valEO.setType("IMG");
// valEO.setItemContent(imgCon);
@@ -2236,7 +2244,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
MultipartFile multipartFile =
new MockMultipartFile(nowFileList.get(0).getName(), nowFileList.get(0).getName(), "text/plain", input);
//文件存入文件表
OSSFile ossFile = ossFileService.uploadLocal(multipartFile, "", null,null);
OSSFile ossFile = ossFileService.uploadLocalOfCos(multipartFile, "", null,null);
if (ObjectUtils.isNotEmpty(ossFile)) {
sb.append(ossFile.getId() + ",");
}
@@ -3122,6 +3130,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
int tableIndex = page.getTableIndex();
int imgIndex = page.getImgIndex();
List<FileSplitValExportDto> valContent = new ArrayList<>();
BASE64Decoder base64Decoder = new BASE64Decoder();
if (getValList != null && !getValList.isEmpty()) {
String preType = "";
for(SarFileSplitItemsValEO itemsValEO : getValList){
@@ -3158,7 +3167,21 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
valContent.add(lastOne);
FileSplitValImgExportDto fileSplitValImgExportDto = new FileSplitValImgExportDto();
fileSplitValImgExportDto.setImgName(imgUUIndex+".png");
fileSplitValImgExportDto.setImgPath(imgContent.substring(imgContent.lastIndexOf(File.separator)+1));
String url = imgContent.substring(imgContent.lastIndexOf("=")+1);
byte[] bytes = new byte[0];
try {
bytes = base64Decoder.decodeBuffer(url);
url = new String(bytes, "utf-8");
fileSplitValImgExportDto.setImgPath(url);
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// fileSplitValImgExportDto.setImgPath(imgContent.substring(imgContent.lastIndexOf(File.separator)+1));
allImgList.add(fileSplitValImgExportDto);
imgIndex++;
page.setImgIndex(imgIndex);
@@ -3191,11 +3214,11 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
public void downLoadImgList(List<FileSplitValImgExportDto> allImgList, String fileNowPath) throws IOException {
for(FileSplitValImgExportDto imgExportDto : allImgList){
String oldPath = uploadpath+ File.separator + imgExportDto.getImgPath();
String oldPath = imgExportDto.getImgPath();
String newPath = fileNowPath + File.separator + imgExportDto.getImgName();
File oldFile = new File(oldPath);
if (oldFile.exists()){
copyFile1(oldPath, newPath);
if (CosBootUtil.doesObjectExist(oldPath)){
InputStream in = CosBootUtil.download(oldPath);
copyFile1(in, newPath);
}
}
@@ -3206,15 +3229,16 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl<FileSplitItemsEOMap
String oldPath = fileExportDto.getUrl();
String newPath = fileNowPath + File.separator + fileExportDto.getFileName();
File oldFile = new File(oldPath);
if (oldFile.exists()){
copyFile1(oldPath, newPath);
if (CosBootUtil.doesObjectExist(oldPath)){
InputStream in = CosBootUtil.download(oldPath);
copyFile1(in, newPath);
}
}
}
public void copyFile1(String srcPath, String destPath) throws IOException {
BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(Paths.get(srcPath)));
public void copyFile1(InputStream in, String destPath) throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos =
new BufferedOutputStream(Files.newOutputStream(Paths.get(destPath),
StandardOpenOption.CREATE,
@@ -414,11 +414,12 @@ local-tool:
uri: http://139.9.235.66:9022
opensso:
# TEST版
# authorizeUrl: https://signin-test.nio.com/oauth2/authorize
# accessTokenUrl: https://signin-test.nio.com/oauth2/accessToken
# profileUrl: https://signin-test.nio.com/oauth2/profile
# clientId: 100679
# clientSecret: CDf2D9404C6ac1B0f7c3e3845ae0282a
# redirectUri: http%3A%2F%2F139.9.235.66%3A8010
# redirectUri: http%3A%2F%2Flocalhost%3A3000
# PROD版
authorizeUrl: https://signin.nio.com/oauth2/authorize
@@ -426,7 +427,7 @@ opensso:
profileUrl: https://signin.nio.com/oauth2/profile
clientId: 100679
clientSecret: 7C3F03170E3ea489df04Ce8DEC7Df4f7
redirectUri: http%3A%2F%2F139.9.235.66%3A8010
redirectUri: http%3A%2F%2Flocalhost%3A3000
---
## space系统