大主宰txt
一 : 构建content 章节类
package com.telehot.studentsign.model;
import com.telehot.platform.app.jpa.hibernate.IdEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* 版权:(C) 版权所有 2000-2014 上*****公司
* <简述>
* <详细描述>
*
* @author ***
* @see
* @since
*/
@Entity
@Table(name = "th_edu_book_content")
public class BookContent extends IdEntity<Long> {
/**
* @Fields bookId : 书id
*/
private Long bookId;
/**
* @Fields chapter : 章节名
*/
private String chapter;
/**
* @Fields content : 内容
*/
private String content;
/**
* @Fields number : 章节序号
*/
private Integer number;
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getChapter() {
return chapter;
}
public void setChapter(String chapter) {
this.chapter = chapter;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
二 :读取txt 解析 ,并存库(这里解析章节过多太耗时,我就只解析300章)
package com.telehot.studentsign.utils;
import com.telehot.studentsign.model.Book;
import com.telehot.studentsign.model.BookContent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 版权:(C) 版权所有 ****分公司
* <简述>
* <详细描述>
*
* @author sjk
* @see
* @since
*/
public class ParseBookContentUtil {
/**
*〈简述〉解析章节
*〈详细描述〉
* @author sjk
* @param book Book book
*/
public static ArrayList<BookContent> parse(Book book) {
String path = book.getBookDownPath();
ArrayList<BookContent> list = new ArrayList<>();
if (!path.contains("G:/books/")) {
path = "G:/books" + path.substring(6);
}
try {
// 编码格式
String encoding = "GBK";
// 文件路径
File file = new File(path);
if (file.isFile() && file.exists()) { // 判断文件是否存在
// 输入流
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
Long count = (long) 0;
boolean bflag=false;
int n=0;
String newStr=null;
String titleName=null;
String newChapterName = null;//新章节名称
String beforeChapterName = null; // 记录上一章 章节名
String substring=null;
int indexOf=0;
int indexOf1=0;
int line=0;
//小说内容类
BookContent content;
while ((lineTxt = bufferedReader.readLine()) != null) {
content=new BookContent();
//小说名称
// content.setName(book.getBookDownName());
count++;
// 正则表达式
Pattern p = Pattern.compile("(^\\s*第)(.{1,9})[章节卷集部篇回](\\s{1})(.*)($\\s*)");
Matcher matcher = p.matcher(lineTxt);
Matcher matcher1 = p.matcher(lineTxt);
newStr=newStr + lineTxt;
while (matcher.find()) {
titleName = matcher.group();
//章节去空
newChapterName = titleName.trim();
//获取章节
//System.out.println(newChapterName);
content.setChapter(beforeChapterName);
beforeChapterName = newChapterName;
indexOf1=indexOf;
//System.out.println(indexOf);
indexOf = newStr.indexOf(newChapterName);
// System.out.println(newChapterName + ":" + "第" + count + "行"); // 得到返回的章
if(bflag) {
bflag=false;
break;
}
if(n==0) {
indexOf1 = newStr.indexOf(newChapterName);
}
n=1;
bflag=true;
//System.out.println(chapter);
}
while(matcher1.find()) {
// 若indexOf1 < indexOf 就说明图书有章节出错,此时跳过 到时在后台补加
// if(indexOf!=indexOf1 ) {
if (indexOf1 < indexOf) {
//根据章节数量生成
if (line > 300) {
return list;
}
content.setNumber(++line);
System.out.println("--" + indexOf1 + "截取" + indexOf + "--");
substring = newStr.substring(indexOf1, indexOf);
//System.out.println(substring);
content.setContent(substring);
content.setBookId(book.getId());
list.add(content);
//bookContentManager.save(content);
System.out.println(content.toString());
} else {
System.out.println("错误章节:" + newChapterName);
}
}
}
bufferedReader.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return list;
}
}
利用 js 展示 章节
<%@page language="java" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/include/taglibs.jsp"%>
<!DOCTYPE html>
<html lang="zh" class="no-js">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="${ctx}/libs/bookread/css/jquery.jscrollpane.custom.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/libs/bookread/css/bookblock.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/libs/bookread/css/custom.css" />
<script src="${ctx}/libs/bookread/js/modernizr.custom.79639.js"></script>
</head>
<body>
<div id="container" class="container">
<div class="menu-panel">
<h3>${book.bookName}</h3>
<ul id="menu-toc" class="menu-toc">
<c:forEach items="${contents}" var="bookConent" varStatus="index">
<c:if test="${index.index eq 0}">
<li class="menu-toc-current"><a href="#${bookConent.id}">${bookConent.chapter}</a></li>
</c:if>
<c:if test="${index.index ne 0}">
<li><a href="#${bookConent.id}">${bookConent.chapter}</a></li>
</c:if>
</c:forEach>
<%-- <li class="menu-toc-current"><a href="#item1">Self-destruction</a></li>
<li><a href="#item2">Why we die</a></li>
<li><a href="#item3">The honeymoon</a></li>
<li><a href="#item4">A drawing joke</a></li>
<li><a href="#item5">Commencing practice</a></li>--%>
</ul>
</div>
<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
<c:forEach items="${contents}" var="bookConent">
<div class="bb-item" id="${bookConent.id}">
<div class="content">
<div class="scroller">
<h2>${bookConent.chapter}</h2>
<p>${bookConent.content}</p>
</div>
</div>
</div>
</c:forEach>
</div>
<nav>
<span id="bb-nav-prev">←</span>
<span id="bb-nav-next">→</span>
</nav>
<span id="tblcontents" class="menu-button">${book.bookName}</span>
</div>
</div><!-- /container -->
<script src="${ctx}/libs/bookread/js/jquery.min.js"></script>
<script src="${ctx}/libs/bookread/js/jquery.mousewheel.js"></script>
<script src="${ctx}/libs/bookread/js/jquery.jscrollpane.min.js"></script>
<script src="${ctx}/libs/bookread/js/jquerypp.custom.js"></script>
<script src="${ctx}/libs/bookread/js/jquery.bookblock.js"></script>
<script src="${ctx}/libs/bookread/js/page.js"></script>
<script>
$(function() {
Page.init();
});
</script>
</body>
</html>
结束!
附件: :js下载~~链接
提取码:jqur