|
@@ -1,7 +1,9 @@
|
|
package com.sf.util;
|
|
package com.sf.util;
|
|
|
|
|
|
|
|
+import com.sf.util.vo.ChapterVo;
|
|
import nl.siegmann.epublib.domain.*;
|
|
import nl.siegmann.epublib.domain.*;
|
|
import nl.siegmann.epublib.epub.EpubReader;
|
|
import nl.siegmann.epublib.epub.EpubReader;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.nodes.Element;
|
|
@@ -10,6 +12,7 @@ import org.jsoup.select.Elements;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
@@ -19,11 +22,13 @@ import java.util.regex.Pattern;
|
|
*/
|
|
*/
|
|
public class EpubUtils {
|
|
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<>();
|
|
|
|
+
|
|
// IO处理的相对路径 是项目的根路径
|
|
// IO处理的相对路径 是项目的根路径
|
|
- File file = new File("epub/长安的荔枝 - 马伯庸.epub");
|
|
|
|
-// File file = new File("epub/《德米安》赫尔曼·黑塞.epub");
|
|
|
|
-// System.out.println(file.exists());
|
|
|
|
|
|
+// File file = new File("epub/长安的荔枝 - 马伯庸.epub");
|
|
|
|
+ File file = new File("epub/" + fileName + ".epub"); // 《德米安》赫尔曼·黑塞.epub
|
|
FileInputStream fis = new FileInputStream(file);
|
|
FileInputStream fis = new FileInputStream(file);
|
|
// 是通过将输入流传入epubreader 来获取它所识别出的book对象
|
|
// 是通过将输入流传入epubreader 来获取它所识别出的book对象
|
|
EpubReader epubReader = new EpubReader();
|
|
EpubReader epubReader = new EpubReader();
|
|
@@ -34,20 +39,33 @@ public class EpubUtils {
|
|
Spine spine = book.getSpine();
|
|
Spine spine = book.getSpine();
|
|
// 获取骨骼所关联的数据
|
|
// 获取骨骼所关联的数据
|
|
List<SpineReference> spineReferences = spine.getSpineReferences();
|
|
List<SpineReference> spineReferences = spine.getSpineReferences();
|
|
|
|
+ // 如果标题和内容不在同一个页面中
|
|
|
|
+ // 要么是标题 要么是内容 此时不适合用lambda表达式
|
|
|
|
+// ChapterVo newVo = null;
|
|
spineReferences.forEach(spineReference -> {
|
|
spineReferences.forEach(spineReference -> {
|
|
// 通过关联数据拿到资源
|
|
// 通过关联数据拿到资源
|
|
Resource resource = spineReference.getResource();
|
|
Resource resource = spineReference.getResource();
|
|
try {
|
|
try {
|
|
byte[] data = resource.getData();
|
|
byte[] data = resource.getData();
|
|
String html = new String(data);
|
|
String html = new String(data);
|
|
-// System.out.println(html);
|
|
|
|
// 首先通过jsoup将页面解析成 文档对象
|
|
// 首先通过jsoup将页面解析成 文档对象
|
|
Document document = Jsoup.parse(html);
|
|
Document document = Jsoup.parse(html);
|
|
Element body = document.body();
|
|
Element body = document.body();
|
|
String text = body.text();
|
|
String text = body.text();
|
|
String htmlTxt = body.html();
|
|
String htmlTxt = body.html();
|
|
-
|
|
|
|
|
|
+ // 因为使用的是lambda表达式 所以这里不能用continue
|
|
|
|
+ // -> {} 这是一个方法体
|
|
|
|
+ if (StringUtils.isBlank(text)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
StringBuffer buffer = new StringBuffer();
|
|
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");
|
|
Elements elements = body.select("p");
|
|
for (Element element : elements) {
|
|
for (Element element : elements) {
|
|
// String pHtml = element.html();
|
|
// String pHtml = element.html();
|
|
@@ -58,12 +76,24 @@ public class EpubUtils {
|
|
}
|
|
}
|
|
// 这就是最终需要的数据
|
|
// 这就是最终需要的数据
|
|
String htmlData = buffer.toString();
|
|
String htmlData = buffer.toString();
|
|
- System.out.println();
|
|
|
|
|
|
+ if(StringUtils.isBlank(htmlData)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ // 如果标题和内容不在同一个页面中
|
|
|
|
+ // 那么判断是标题 就创建对象
|
|
|
|
+ // 是内容 就存储对象
|
|
|
|
+// newVo = new ChapterVo();
|
|
|
|
+// newVo.setContent("");
|
|
|
|
+// chapterVoList.add(newVo);
|
|
|
|
+
|
|
|
|
+ ChapterVo chapterVo = ChapterVo.builder().title(title).content(htmlData).build();
|
|
|
|
+ chapterVoList.add(chapterVo);
|
|
|
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+ return chapterVoList;
|
|
}
|
|
}
|
|
|
|
|
|
// 移除标签
|
|
// 移除标签
|