diff --git a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java index 9dd5c46..fb97676 100644 --- a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java +++ b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java @@ -44,10 +44,7 @@ import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath; import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathPara; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLevelText; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; @@ -151,17 +148,10 @@ public class FileSpiltService { } String partName = ""; -// LawsDomesticStandard lawsDomesticStandard = lawsDomesticStandardService.getById(sarFileSplitInfoEO.getStandardId()); -// LawsEnterpriseStandard lawsEnterpriseStandard = lawsEnterpriseStandardService.getById(sarFileSplitInfoEO.getStandardId()); -// if (!Objects.isNull(lawsDomesticStandard)){ -// partName = lawsDomesticStandard.getPartName(); -// }else { -// partName = lawsEnterpriseStandard.getPartName(); -// } + // 拆分文件条款表中最终插入数据 List messageList = new ArrayList<>(); -// List messageList = new ArrayList<>(); int messageNumber = 0; // 拆分文件目录表中最终插入数据 List treeList = new ArrayList<>(); @@ -171,7 +161,6 @@ public class FileSpiltService { String generalCatalogueId = UUIDUtils.randomUUID20(); SarFileSplitMenuEO documentTreeEO = new SarFileSplitMenuEO(); -// SarFileSplitMenuEO documentTreeEO = new SarFileSplitMenuEO(generalCatalogueId,"总目录","",sarFileSplitInfoEO.getId(),treeListDisplay++,""); documentTreeEO.setId(generalCatalogueId); documentTreeEO.setName("总目录"); documentTreeEO.setInfoId(sarFileSplitInfoEO.getId()); @@ -183,15 +172,11 @@ public class FileSpiltService { treeList.add(documentTreeEO); List nowStart = new ArrayList<>(); -// if (sarFileSplitInfoEO.getStartNumber()<4){ //从第二个开始拆解 nowStart.add("2"); -// } else { -// nowStart.add(String.valueOf(sarFileSplitInfoEO.getStartNumber())); -// } -// 获取文件地址 - + //获取文件地址 InputStream splitFileIs = null; + InputStream splitFileIs1 = null; if (StringUtils.equals(splitSource, CommonConstant.SPLIT_SOURCE_1)) { String fileId = sarFileSplitInfoEO.getFileId(); @@ -209,9 +194,11 @@ public class FileSpiltService { throw new JeroBootException("当前文件未找到,请重新选择!"); } splitFileIs = MinioUtil.download(readFilePath); + splitFileIs1 = MinioUtil.download(readFilePath); } else if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ try { splitFileIs = new FileInputStream(readFilePath); + splitFileIs1 = new FileInputStream(readFilePath); } catch (FileNotFoundException e) { e.printStackTrace(); throw new JeroBootException("当前文件未找到,请重新选择!"); @@ -236,14 +223,13 @@ public class FileSpiltService { HttpEntity> requestEntity = new HttpEntity<>(params, headers); // 发送POST请求并获取响应 -// ResponseEntity responseEntity = restTemplate.postForEntity(bydDownLoadFileUrl, requestEntity, byte[].class); - Long fileIdLong = Long.valueOf(sarFileSplitInfoEO.getId()); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(bydDownLoadFileUrl).queryParam("fileId", fileIdLong); // 发送POST请求并获取响应 ResponseEntity responseEntity = restTemplate.postForEntity(builder.toUriString(), requestEntity, byte[].class); // 获取文件流 splitFileIs = new ByteArrayInputStream(responseEntity.getBody()); + splitFileIs1 = new ByteArrayInputStream(responseEntity.getBody()); }catch (Exception ex){ ex.printStackTrace(); log.error("调用比亚迪下载文档接口失败:" + ex.getMessage()); @@ -252,9 +238,7 @@ public class FileSpiltService { log.info("下载文档结束========================================================="); } - try (InputStream is = splitFileIs;InputStream is1 = splitFileIs) { - //String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx"; - //try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。 + try (InputStream is = splitFileIs;InputStream is1 = splitFileIs1) { ZipSecureFile.setMinInflateRatio(-1.0d); XWPFDocument doc = new XWPFDocument(is); // 获取文档中的图片 @@ -271,20 +255,16 @@ public class FileSpiltService { } //获得序号标题和无标题序号 TitleNumberVO titleNumberVO = AsposeUtil.getNumber(is1); - //TitleNumberVO titleNumberVO = new TitleNumberVO(); Map titleNumberMap = titleNumberVO.getTitleNumberMap(); List emptyTitleList = titleNumberVO.getEmptyTitleList(); // 记录拆分后的每一段文字 List elements = doc.getBodyElements(); - // 记录word自动生成的编号数据 - Map numberMap = new HashMap(); boolean menuCanAdd = false; // 判断当前目录下是否可以正常填写数据 boolean firstFlag = true; List startDigitList = getStartDigit(elements, enumByValue); String regex = "^[0-9]*$"; String preName = ""; int preNameNum = 0; -// for (IBodyElement element : elements) { for (int g=0; g< elements.size(); g++) { IBodyElement element = elements.get(g); // 段落 @@ -296,29 +276,14 @@ public class FileSpiltService { List childList = runOrMaths.getChildList(); for (Object child : childList) { - //if (child instanceof XWPFRun) { - // //处理段落中的文本以及图片 - // handleParagraphRun(content, (XWPFRun) child, imageParser); - //} else if (child instanceof CTOMath) { CTOMath ctoMath = (CTOMath) child; - //stringBuilder.append(ctoMath.xmlText()); - // 处理word中存在的公式成mathML格式 - //String imageName = OmmlUtils.convertOmathToPng(ctoMath.xmlText()); - //if (messageList.size() > 0) { - // addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, imageName); - //} File mathToImgFile = this.getMathToImgUrl(ctoMath.xmlText()); if (null != mathToImgFile && messageList.size() > 0) { addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, mathToImgFile); } } else if (child instanceof CTOMathPara) { CTOMathPara ctoMathPara = (CTOMathPara) child; - //String imageName = OmmlUtils.convertOmathToPng(ctoMathPara.xmlText()); - //if (messageList.size() > 0) { - // addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, imageName); - //} - File mathToImgFile = this.getMathToImgUrl(ctoMathPara.xmlText()); if (null != mathToImgFile && messageList.size() > 0) { addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, mathToImgFile); @@ -329,6 +294,9 @@ public class FileSpiltService { for (XWPFRun xwrun : p.getRuns()) { VerticalAlign subscript = xwrun.getSubscript(); String smalltext = xwrun.getText(0); + if (StringUtils.isNotBlank(smalltext)){ + smalltext = smalltext.replace("<", "<").replace(">", ">"); + } if (smalltext==null){ continue; } @@ -346,6 +314,7 @@ public class FileSpiltService { stringBuilder.append(smalltext); } } + ParagraphAlignment alignment = p.getAlignment(); // 处理段落生成编号不识别问题 String paragraphString = stringBuilder.toString(); if (StringUtils.isNotBlank(paragraphString)) { @@ -369,9 +338,9 @@ public class FileSpiltService { if ("前言".equals(getChinese(paragraphString)) && (paragraphString - .replaceAll("\\s", "") - .replaceAll(Character.toString((char) 8195),"") - .length() == 2)) { // 前言后面有文本,判定为是目录中的前言 + .replaceAll("\\s", "") + .replaceAll(Character.toString((char) 8195),"") + .length() == 2)) { // 前言后面有文本,判定为是目录中的前言 firstFlag = false; } if(firstFlag) { @@ -397,7 +366,6 @@ public class FileSpiltService { List clauseContent = new ArrayList<>(); SarFileSplitItemsEO message = getSarFileSplitItemsEO(sarFileSplitInfoEO.getId(), "1", "范围", clauseContent); SarFileSplitMenuEO documentTreeEO1 = new SarFileSplitMenuEO(); -// SarFileSplitMenuEO documentTreeEO1 = new SarFileSplitMenuEO(UUIDUtils.randomUUID20(),message.getItemsNum(),generalCatalogueId,sarFileSplitInfoEO.getId(),treeListDisplay++,"范围"); documentTreeEO1.setId(UUIDUtils.randomUUID20()); documentTreeEO1.setItemName("范围"); documentTreeEO1.setPId(generalCatalogueId); @@ -405,7 +373,6 @@ public class FileSpiltService { documentTreeEO1.setDisplaySeq(treeListDisplay++); documentTreeEO1.setName(message.getItemNum()); documentTreeEO1.setValidFlag(0); -// treeList.add(documentTreeEO1); message.setMenuId(documentTreeEO1.getId()); if (sarFileSplitInfoEO.getStartNumber() <= 1 && sarFileSplitInfoEO.getStopNumber() >= 1) { @@ -443,7 +410,6 @@ public class FileSpiltService { startDigit = getOneStartDigit(paragraphString, enumByValue); } for (int i = 0; i < nowStart.size(); i++) { -// if (paragraphString.startsWith(nowStart.get(i))) { if (startDigit.equals(nowStart.get(i))) { // 判断真的是标题还是干扰项 TODO if (nowStart.get(i).matches(regex)) { @@ -474,7 +440,7 @@ public class FileSpiltService { preNameNum = g; message.setMenuId(sarFileSplitMenuEO.getId()); if(!StringUtils.isEmpty(itemName) && !paragraphString.endsWith(itemName)){ - addItermsConditionsText(message, itemValList, paragraphString.substring(nowStart.get(i).length())); + addItermsConditionsText(message, itemValList, paragraphString.substring(nowStart.get(i).length()), alignment); } message.setApplicableComponents(partName); messageList.add(message); @@ -489,27 +455,21 @@ public class FileSpiltService { // 判断是否是附录 boolean find = APPENDIX_PATTERN.matcher(paragraphString).find(); // 获取当前最后一项num - String appendixNum = messageList.get(messageList.size() - 1).getItemNum().split("\\.")[0]; if (find && !messageList.get(messageList.size() - 1).getItemNum().equals("附录")) { - if (Integer.valueOf(appendixNum) + 1 <= sarFileSplitInfoEO.getStopNumber()) { - String itemName = "附录"; - List clauseContent = new ArrayList<>(); - clauseContent.add(paragraphString); - SarFileSplitItemsEO message = getSarFileSplitItemsEO(sarFileSplitInfoEO.getId(), "附录", itemName, clauseContent); - // 增加一条数据,需要在数结构中加一条数据 - SarFileSplitMenuEO sarFileSplitMenuEO = getTreeList("附录", treeList, generalCatalogueId, sarFileSplitInfoEO, treeListDisplay++, "", enumByValue); - addItermsConditionsText(message, itemValList, paragraphString); - treeList.add(sarFileSplitMenuEO); - message.setMenuId(sarFileSplitMenuEO.getId()); - message.setApplicableComponents(partName); - messageList.add(message); - nowStart = new ArrayList<>(); - } else { - break; - } + String itemName = paragraphString; + List clauseContent = new ArrayList<>(); + clauseContent.add(paragraphString); + SarFileSplitItemsEO message = getSarFileSplitItemsEO(sarFileSplitInfoEO.getId(), itemName, "", clauseContent); + // 增加一条数据,需要在数结构中加一条数据 + SarFileSplitMenuEO sarFileSplitMenuEO = getTreeList(itemName, treeList, generalCatalogueId, sarFileSplitInfoEO, treeListDisplay++, "", enumByValue); + treeList.add(sarFileSplitMenuEO); + message.setMenuId(sarFileSplitMenuEO.getId()); + message.setApplicableComponents(partName); + messageList.add(message); + nowStart = new ArrayList<>(); } else { if (!paragraphString.equals("")) { - addItermsConditionsText(messageList.get(messageList.size() - 1), itemValList, paragraphString); + addItermsConditionsText(messageList.get(messageList.size() - 1), itemValList, paragraphString, alignment); } List imageBundleList = readImageInParagraph((XWPFParagraph) p); if (CollectionUtils.isNotEmpty(imageBundleList)) { @@ -531,11 +491,9 @@ public class FileSpiltService { } } catch (Exception e) { - log.info("拆分出现异常了:",e); log.error(e.getMessage(),e); return -1; } - log.info("treeList长度:" + treeList.size()); if (treeList.size() > 0 && messageList.size() > 0) { sarFileSplitMenuEOMapper.insertForeach(treeList); for (int i = 0; i < messageList.size(); i++) { @@ -549,7 +507,6 @@ public class FileSpiltService { return -1; } return treeList.size(); - } private String getNextParagraphString(int g, List elements, SplitFileTypeTypeEnum enumByValue) { @@ -586,7 +543,6 @@ public class FileSpiltService { * @return */ public List getNewNowStart(String nowStart) { -// List nowStartList = new ArrayList<>(); List nowResultStartList = new ArrayList<>(); String[] numberStr = nowStart.split("\\."); String startnumber = ""; @@ -647,18 +603,7 @@ public class FileSpiltService { } break; -// case EU: -// if (name.indexOf(".") >= 0) { -// if (name.substring(0, name.lastIndexOf(".") - 1).equals(forDocumentTree.getName())) { -// documentTreeEO.setPId(forDocumentTree.getId()); -//// pIdList.add(forDocumentTree.getId()); -// break a; -// } -// } else { -// documentTreeEO.setPId(generalCatalogueId); -//// pIdList.add(generalCatalogueId); -// } -// break; + case JPN_ATTACHMENT: if (name.contains(".")) { if (name.indexOf(".") >= 0) { @@ -1123,9 +1068,13 @@ public class FileSpiltService { } // 像每个条款中依次插入每一段的内容 - public static void addItermsConditionsText(SarFileSplitItemsEO message, List itemsValList, String p) { + public static void addItermsConditionsText(SarFileSplitItemsEO message, List itemsValList, String p, ParagraphAlignment alignment) { message.getItemsCondi().add(p); - message.setItemContent(message.getItemContent() + "

" + p + "

"); + if (ParagraphAlignment.CENTER.equals(alignment)){ + message.setItemContent(message.getItemContent() + "

" + p + "

"); + }else { + message.setItemContent(message.getItemContent() + "

" + p + "

"); + } itemsValList.add(getSplitItemsValObject(message.getId(), SplitFilePragraTypeEnum.TEXT.getValue(), p, message.getItemsCondi().size(), null)); } @@ -1154,12 +1103,6 @@ public class FileSpiltService { StringBuilder tableToHtmlStr = new StringBuilder(""); for (int i = 0; i < tableRowsSize; i++) { - //最后一行的都增加下边框 - //if (i == tableRowsSize-1){ - // tableToHtmlStr.append(""); - //}else { - // tableToHtmlStr.append(""); - //} tableToHtmlStr.append(""); int tableCellsSize = table.getRow(i).getTableCells().size(); @@ -1196,6 +1139,100 @@ public class FileSpiltService { List paragraphs = tableCell.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { List imageBundleList = readImageInParagraph(paragraph); + ParagraphChildOrderManager runOrMaths = new ParagraphChildOrderManager(paragraph); + List childList = runOrMaths.getChildList(); + for (Object child : childList) { + if (child instanceof CTOMath) { + CTOMath ctoMath = (CTOMath) child; + File mathToImgFile = this.getMathToImgUrl(ctoMath.xmlText()); + if (null != mathToImgFile) { + String imageName = UUIDUtils.randomUUID10() + mathToImgFile.getName(); + String path = splitUrl + imageName; + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + File splitImgFolder = new File(splitImgPath); + if (!splitImgFolder.exists()) { + splitImgFolder.mkdirs(); + } + + String newFilePath = splitImgPath + imageName; + try { + File newFile = new File(newFilePath); + FileUtils.copyFile(mathToImgFile, newFile); + } catch (IOException e) { + e.printStackTrace(); + } + + //上传成功 进行数据库存储 + OSSFile ossFile = new OSSFile(); + // 文件名 + String fileName = mathToImgFile.getName(); + fileName = CommonUtils.getFileName(fileName); + ossFile.setFileName(fileName); + ossFile.setUrl(newFilePath); + ossFile.setId(imageName); + iossFileService.save(ossFile); + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ + byte[] bytev = null; + try (FileInputStream fis = new FileInputStream(mathToImgFile)){ + bytev = new byte[(int) mathToImgFile.length()]; + fis.read(bytev); + }catch (IOException e){ + e.printStackTrace(); + } + String filepathandname = filePathCos + "/" + imageName; + MinioUtil.uploadByte(bytev, filepathandname); + } + + String imgConVal = ""; + stringBuilder.append(imgConVal); + } + } else if (child instanceof CTOMathPara) { + CTOMathPara ctoMathPara = (CTOMathPara) child; + File mathToImgFile = this.getMathToImgUrl(ctoMathPara.xmlText()); + if (null != mathToImgFile) { + String imageName = UUIDUtils.randomUUID10() + mathToImgFile.getName(); + String path = splitUrl + imageName; + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + File splitImgFolder = new File(splitImgPath); + if (!splitImgFolder.exists()) { + splitImgFolder.mkdirs(); + } + + String newFilePath = splitImgPath + imageName; + try { + File newFile = new File(newFilePath); + FileUtils.copyFile(mathToImgFile, newFile); + } catch (IOException e) { + e.printStackTrace(); + } + + //上传成功 进行数据库存储 + OSSFile ossFile = new OSSFile(); + // 文件名 + String fileName = mathToImgFile.getName(); + fileName = CommonUtils.getFileName(fileName); + ossFile.setFileName(fileName); + ossFile.setUrl(newFilePath); + ossFile.setId(imageName); + iossFileService.save(ossFile); + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ + byte[] bytev = null; + try (FileInputStream fis = new FileInputStream(mathToImgFile)){ + bytev = new byte[(int) mathToImgFile.length()]; + fis.read(bytev); + }catch (IOException e){ + e.printStackTrace(); + } + String filepathandname = filePathCos + "/" + imageName; + MinioUtil.uploadByte(bytev, filepathandname); + } + + String imgConVal = ""; + stringBuilder.append(imgConVal); + } + } + } + if (CollectionUtils.isNotEmpty(imageBundleList)) { String s = addTableImage(paragraph, imageBundleList); stringBuilder.append(s); @@ -1487,7 +1524,7 @@ public class FileSpiltService { // 此处相当于在Authorization里头添加Bear token参数信息 HttpResponse res = client.execute(post); String results = EntityUtils.toString(res.getEntity()); - + System.out.println("getMathToImgUrl results:"+results); if (StringUtils.isNotEmpty(results)) { boolean errorFlag = (StringUtils.equals(results,"非法请求!") || StringUtils.equals(results,"非法参数!") || StringUtils.equals(results,"转换失败!")); if (!errorFlag) {