电子书拆分修改
This commit is contained in:
@@ -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) {
|
||||||
|
try{
|
||||||
|
int topText = 22;
|
||||||
|
int secText = 16;
|
||||||
BookEntity bookEntity = this.getBaseMapper().selectById(bookId);
|
BookEntity bookEntity = this.getBaseMapper().selectById(bookId);
|
||||||
String novel = bookEntity.getNovel();
|
String novel = bookEntity.getNovel();
|
||||||
InputStream inputStream = FileDownloadUtil.getInputStream(novel);
|
InputStream inputStream = FileDownloadUtil.getInputStream(novel);
|
||||||
Document doc = new Document(inputStream);
|
XWPFDocument document = new XWPFDocument(inputStream);
|
||||||
int index = 0;
|
// 遍历文档中的所有元素(段落和表格)
|
||||||
int x1 = 0;
|
List<IBodyElement> bodyElements = document.getBodyElements();
|
||||||
int x2 = 0;
|
|
||||||
String chapterName = "";
|
String chapterName = "";
|
||||||
int chapterId = 0;
|
int bookChapterId = 0;
|
||||||
for (int i = 0; i < doc.getSections().getCount(); i++) {
|
int number = 0;
|
||||||
Section section = doc.getSections().get(i);
|
for (int i=0;i < bodyElements.size(); i++) {
|
||||||
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) {
|
IBodyElement element = bodyElements.get(i);
|
||||||
Paragraph paragraph = section.getParagraphs().get(j);
|
if (element instanceof XWPFParagraph) {
|
||||||
BookChapterEntity chapter = new BookChapterEntity();
|
XWPFParagraph paragraph = (XWPFParagraph) element;
|
||||||
chapter.setBookId(bookId);
|
String text = paragraph.getText();
|
||||||
chapter.setNumber(index++);
|
if (text != null && !text.isEmpty()) {//处理段落或正文
|
||||||
chapter.setSort(j);
|
if (paragraph.getRuns().get(0).getFontSize()>=topText){//判断是否是顶级标题
|
||||||
if (paragraph.getStyleName().equals("Heading1")&&StringUtils.isNotBlank(paragraph.getText())) {
|
chapterName = text;
|
||||||
x1 = j;
|
BookChapterEntity bookChapter = new BookChapterEntity();
|
||||||
chapterName = paragraph.getText();
|
bookChapter.setBookId(bookId);
|
||||||
}else if (paragraph.getStyleName().equals("Heading2")&&StringUtils.isNotBlank(paragraph.getText())){
|
bookChapter.setChapter(text);
|
||||||
x2 = j;
|
bookChapter.setNumber(number++);
|
||||||
chapter.setChapter(chapterName);
|
for (int j=1;j<bodyElements.size()-i;j++){//判断下一段落是二级标题还是正文,正文直接保存
|
||||||
chapter.setContent(paragraph.getText());
|
IBodyElement e = bodyElements.get(i+j);
|
||||||
System.out.println(paragraph.getText());
|
if (e instanceof XWPFParagraph) {
|
||||||
bookChapterService.save(chapter);
|
XWPFParagraph p = (XWPFParagraph) e;
|
||||||
chapterId = chapter.getId();
|
if (p.getText() != null && !p.getText().isEmpty()){
|
||||||
}else if (StringUtils.isNotBlank(paragraph.getText())){
|
if (p.getRuns().get(0).getFontSize()<secText){
|
||||||
if (x1 > x2){
|
bookChapterService.save(bookChapter);
|
||||||
chapter.setChapter(chapterName);
|
bookChapterId = bookChapter.getId();
|
||||||
chapter.setContent(paragraph.getText());
|
}
|
||||||
System.out.println(paragraph.getText());
|
break;
|
||||||
bookChapterService.save(chapter);
|
|
||||||
}else {
|
|
||||||
BookChapterContentEntity content = new BookChapterContentEntity();
|
|
||||||
content.setBookId(bookId);
|
|
||||||
content.setBookChatperId(chapterId);
|
|
||||||
content.setNumber(index);
|
|
||||||
content.setContent(paragraph.getText());
|
|
||||||
bookChapterContentService.save(content);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}else if (paragraph.getRuns().get(0).getFontSize()>=secText){//判断是否是二级标题
|
||||||
|
if (StringUtils.isNotBlank(chapterName)){
|
||||||
|
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user