|
@@ -1,7 +1,9 @@
|
|
|
package com.sf.util;
|
|
|
|
|
|
+import com.sf.util.vo.ChapterVo;
|
|
|
import nl.siegmann.epublib.domain.*;
|
|
|
import nl.siegmann.epublib.epub.EpubReader;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.jsoup.Jsoup;
|
|
|
import org.jsoup.nodes.Document;
|
|
|
import org.jsoup.nodes.Element;
|
|
@@ -10,6 +12,7 @@ import org.jsoup.select.Elements;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -19,11 +22,13 @@ import java.util.regex.Pattern;
|
|
|
*/
|
|
|
public class EpubUtils {
|
|
|
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
+
|
|
|
+ public static List<ChapterVo> getChapterInfo(String fileName) throws Exception {
|
|
|
+ List<ChapterVo> chapterVoList = new ArrayList<>();
|
|
|
+
|
|
|
|
|
|
- File file = new File("epub/长安的荔枝 - 马伯庸.epub");
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ File file = new File("epub/" + fileName + ".epub");
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
|
|
EpubReader epubReader = new EpubReader();
|
|
@@ -34,20 +39,33 @@ public class EpubUtils {
|
|
|
Spine spine = book.getSpine();
|
|
|
|
|
|
List<SpineReference> spineReferences = spine.getSpineReferences();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
spineReferences.forEach(spineReference -> {
|
|
|
|
|
|
Resource resource = spineReference.getResource();
|
|
|
try {
|
|
|
byte[] data = resource.getData();
|
|
|
String html = new String(data);
|
|
|
-
|
|
|
|
|
|
Document document = Jsoup.parse(html);
|
|
|
Element body = document.body();
|
|
|
String text = body.text();
|
|
|
String htmlTxt = body.html();
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(text)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
+
|
|
|
+ String title = "";
|
|
|
+ Elements h1Ele = body.select("h1");
|
|
|
+ if (h1Ele.size() > 0) {
|
|
|
+ title = h1Ele.get(0).text();
|
|
|
+ }
|
|
|
+
|
|
|
Elements elements = body.select("p");
|
|
|
for (Element element : elements) {
|
|
|
|
|
@@ -58,12 +76,24 @@ public class EpubUtils {
|
|
|
}
|
|
|
|
|
|
String htmlData = buffer.toString();
|
|
|
- System.out.println();
|
|
|
+ if(StringUtils.isBlank(htmlData)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ChapterVo chapterVo = ChapterVo.builder().title(title).content(htmlData).build();
|
|
|
+ chapterVoList.add(chapterVo);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
});
|
|
|
+ return chapterVoList;
|
|
|
}
|
|
|
|
|
|
|