电子书拆分修改

This commit is contained in:
wuchunlei
2024-11-26 15:11:58 +08:00
parent 708e925bf0
commit 3fc7a80580

View File

@@ -27,13 +27,15 @@ import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.Paragraph;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.io.BufferedReader; import javax.imageio.ImageIO;
import java.io.InputStream; import java.awt.image.BufferedImage;
import java.io.InputStreamReader; import java.io.*;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -450,53 +452,113 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
*/ */
@Override @Override
public boolean getWordSection(Integer bookId) { public boolean getWordSection(Integer bookId) {
BookEntity bookEntity = this.getBaseMapper().selectById(bookId); try{
String novel = bookEntity.getNovel(); int topText = 22;
InputStream inputStream = FileDownloadUtil.getInputStream(novel); int secText = 16;
Document doc = new Document(inputStream); BookEntity bookEntity = this.getBaseMapper().selectById(bookId);
int index = 0; String novel = bookEntity.getNovel();
int x1 = 0; InputStream inputStream = FileDownloadUtil.getInputStream(novel);
int x2 = 0; XWPFDocument document = new XWPFDocument(inputStream);
String chapterName = ""; // 遍历文档中的所有元素(段落和表格)
int chapterId = 0; List<IBodyElement> bodyElements = document.getBodyElements();
for (int i = 0; i < doc.getSections().getCount(); i++) { String chapterName = "";
Section section = doc.getSections().get(i); int bookChapterId = 0;
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) { int number = 0;
Paragraph paragraph = section.getParagraphs().get(j); for (int i=0;i < bodyElements.size(); i++) {
BookChapterEntity chapter = new BookChapterEntity(); IBodyElement element = bodyElements.get(i);
chapter.setBookId(bookId); if (element instanceof XWPFParagraph) {
chapter.setNumber(index++); XWPFParagraph paragraph = (XWPFParagraph) element;
chapter.setSort(j); String text = paragraph.getText();
if (paragraph.getStyleName().equals("Heading1")&&StringUtils.isNotBlank(paragraph.getText())) { if (text != null && !text.isEmpty()) {//处理段落或正文
x1 = j; if (paragraph.getRuns().get(0).getFontSize()>=topText){//判断是否是顶级标题
chapterName = paragraph.getText(); chapterName = text;
}else if (paragraph.getStyleName().equals("Heading2")&&StringUtils.isNotBlank(paragraph.getText())){ BookChapterEntity bookChapter = new BookChapterEntity();
x2 = j; bookChapter.setBookId(bookId);
chapter.setChapter(chapterName); bookChapter.setChapter(text);
chapter.setContent(paragraph.getText()); bookChapter.setNumber(number++);
System.out.println(paragraph.getText()); for (int j=1;j<bodyElements.size()-i;j++){//判断下一段落是二级标题还是正文,正文直接保存
bookChapterService.save(chapter); IBodyElement e = bodyElements.get(i+j);
chapterId = chapter.getId(); if (e instanceof XWPFParagraph) {
}else if (StringUtils.isNotBlank(paragraph.getText())){ XWPFParagraph p = (XWPFParagraph) e;
if (x1 > x2){ if (p.getText() != null && !p.getText().isEmpty()){
chapter.setChapter(chapterName); if (p.getRuns().get(0).getFontSize()<secText){
chapter.setContent(paragraph.getText()); bookChapterService.save(bookChapter);
System.out.println(paragraph.getText()); bookChapterId = bookChapter.getId();
bookChapterService.save(chapter); }
}else { break;
BookChapterContentEntity content = new BookChapterContentEntity(); }
content.setBookId(bookId); }
content.setBookChatperId(chapterId); }
content.setNumber(index); }else if (paragraph.getRuns().get(0).getFontSize()>=secText){//判断是否是二级标题
content.setContent(paragraph.getText()); if (StringUtils.isNotBlank(chapterName)){
bookChapterContentService.save(content); BookChapterEntity bookChapter = new BookChapterEntity();
bookChapter.setBookId(bookId);
bookChapter.setChapter(chapterName);
bookChapter.setContent(text);
bookChapter.setNumber(number++);
bookChapterService.save(bookChapter);
bookChapterId = bookChapter.getId();
}
}else {//正文
if (bookChapterId!=0){
BookChapterContentEntity contentEntity = new BookChapterContentEntity();
contentEntity.setBookId(bookId);
contentEntity.setBookChatperId(bookChapterId);
contentEntity.setContent(text);
contentEntity.setNumber(number++);
bookChapterContentService.save(contentEntity);
}
}
} else {// 顺序遍历图片
List<IRunElement> runs = paragraph.getIRuns();
for (IRunElement run : runs) {
if (run instanceof XWPFRun) {
XWPFRun xWPFRun = (XWPFRun) run;
for (XWPFPicture picture : xWPFRun.getEmbeddedPictures()) {
XWPFPictureData pictureData = picture.getPictureData();
String fileName = pictureData.getFileName();
String url = uploadFile(new ByteArrayInputStream(pictureData.getData()),fileName);
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(pictureData.getData()));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BookChapterContentEntity contentEntity = new BookChapterContentEntity();
contentEntity.setBookId(bookId);
contentEntity.setBookChatperId(bookChapterId);
contentEntity.setContent(url);
contentEntity.setOtherContent(width+","+height);
contentEntity.setNumber(number++);
bookChapterContentService.save(contentEntity);
}
}
}
} }
} }
} }
}catch (Exception e) {
e.printStackTrace();;
} }
return false; return false;
} }
public String uploadFile(InputStream inputStream,String fileName) {
String endpoint = ConstantPropertiesUtils.END_POIND;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
try {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
String datePath = new DateTime().toString("yyyy/MM/dd");
fileName = datePath + "/" + fileName;
ossClient.putObject(bucketName, fileName, inputStream);
ossClient.shutdown();
String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
return url;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override @Override
public PageUtils queryPagebooks(Map<String, Object> params) { public PageUtils queryPagebooks(Map<String, Object> params) {
IPage<BookEntity> page = this.page( IPage<BookEntity> page = this.page(