queryBullet(@RequestParam("contentId") Long contentId) {
@@ -325,27 +359,30 @@ public class BookController {
OutputStream out = resp.getOutputStream();
//设置响应头,对文件进行url编码
bookName = URLEncoder.encode(bookName, "UTF-8");
- resp.setContentType("application/octet-stream");//解决手机端不能下载附件的问题
+ //解决手机端不能下载附件的问题
+ resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "attachment;filename=" + bookName + ".txt");
if (count > 0) {
for (int i = 0; i < count; i++) {
- String index = bookService.queryIndexList(bookId, i);
- String content = bookService.queryContentList(bookId, i);
- out.write(index.getBytes("utf-8"));
- out.write("\n".getBytes("utf-8"));
- content = content.replaceAll("
", "\r\n");
- content = content.replaceAll(" ", " ");
- content = content.replaceAll("]*>", "");
- content = content.replaceAll("", "");
- content = content.replaceAll("]*>", "");
- content = content.replaceAll("
", "");
- content = content.replaceAll("]*>[^<]*]*>[^<]*\\s*
", "");
- content = content.replaceAll("]*>", "");
- content = content.replaceAll("
", "\r\n");
- out.write(content.getBytes("utf-8"));
- out.write("\r\n".getBytes("utf-8"));
- out.write("\r\n".getBytes("utf-8"));
- out.flush();
+ String index = bookService.queryIndexNameByBookIdAndIndexNum(bookId, i);
+ if(index != null) {
+ String content = bookService.queryContentList(bookId, i);
+ out.write(index.getBytes(StandardCharsets.UTF_8));
+ out.write("\n".getBytes(StandardCharsets.UTF_8));
+ content = content.replaceAll("
", "\r\n");
+ content = content.replaceAll(" ", " ");
+ content = content.replaceAll("]*>", "");
+ content = content.replaceAll("", "");
+ content = content.replaceAll("]*>", "");
+ content = content.replaceAll("
", "");
+ content = content.replaceAll("]*>[^<]*]*>[^<]*\\s*
", "");
+ content = content.replaceAll("]*>", "");
+ content = content.replaceAll("
", "\r\n");
+ out.write(content.getBytes(StandardCharsets.UTF_8));
+ out.write("\r\n".getBytes(StandardCharsets.UTF_8));
+ out.write("\r\n".getBytes(StandardCharsets.UTF_8));
+ out.flush();
+ }
}
}
diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/web/UserController.java b/novel-front/src/main/java/xyz/zinglizingli/books/web/UserController.java
index 48eba05..0e8e006 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/books/web/UserController.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/books/web/UserController.java
@@ -1,6 +1,7 @@
package xyz.zinglizingli.books.web;
+import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
@@ -15,21 +16,23 @@ import xyz.zinglizingli.common.cache.CommonCacheUtil;
import java.util.*;
+/**
+ * @author XXY
+ */
@Controller
@RequestMapping("user")
+@RequiredArgsConstructor
public class UserController {
- @Autowired
- private UserService userService;
+ private final UserService userService;
- @Autowired
- private BookService bookService;
-
- @Autowired
- private CommonCacheUtil commonCacheUtil;
+ private final CommonCacheUtil commonCacheUtil;
+ /**
+ * 登陆页面
+ * */
@RequestMapping("login.html")
public String login(Long bookId, ModelMap modelMap) {
modelMap.put("bookId", bookId);
@@ -37,10 +40,13 @@ public class UserController {
}
+ /**
+ * 登陆或注册
+ * */
@RequestMapping("loginOrRegist")
@ResponseBody
public Map loginOrRegist(User user,Long bookId) {
- Map result = new HashMap<>();
+ Map result = new HashMap<>(2);
//查询用户名是否存在
boolean isExistLoginName = userService.isExistLoginName(user.getLoginName());
String token = null;
@@ -77,10 +83,13 @@ public class UserController {
return result;
}
+ /**
+ * 登陆状态查询
+ * */
@RequestMapping("isLogin")
@ResponseBody
public Map isLogin(String token) {
- Map result = new HashMap<>();
+ Map result = new HashMap<>(2);
String userId = commonCacheUtil.get(token);
if(StringUtils.isEmpty(userId)){
result.put("code", -1);
@@ -93,6 +102,9 @@ public class UserController {
}
+ /**
+ * 加入书架
+ * */
@RequestMapping("addToCollect")
@ResponseBody
public Map addToCollect(Long bookId,String token) {
@@ -109,10 +121,13 @@ public class UserController {
return result;
}
+ /**
+ * 撤下书架
+ * */
@RequestMapping("cancelToCollect")
@ResponseBody
public Map cancelToCollect(Long bookId,String token) {
- Map result = new HashMap<>();
+ Map result = new HashMap<>(2);
String userId = commonCacheUtil.get(token);
if(StringUtils.isEmpty(userId)){
result.put("code", -1);
@@ -125,10 +140,13 @@ public class UserController {
return result;
}
+ /**
+ * 判断是否加入书架
+ * */
@RequestMapping("isCollect")
@ResponseBody
public Map isCollect(Long bookId,String token) {
- Map result = new HashMap<>();
+ Map result = new HashMap<>(2);
String userId = commonCacheUtil.get(token);
if(StringUtils.isEmpty(userId)){
result.put("code", -1);
diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/config/ErrorConfig.java b/novel-front/src/main/java/xyz/zinglizingli/common/config/ErrorConfig.java
index a765149..d8ebfaf 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/common/config/ErrorConfig.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/common/config/ErrorConfig.java
@@ -5,6 +5,7 @@ import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
+import xyz.zinglizingli.common.utils.Constants;
/**
*定义配置类
@@ -15,8 +16,8 @@ public class ErrorConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage[] errorPages = new ErrorPage[2];
- errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/book/index.html");
- errorPages[1] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/mang.html");
+ errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, Constants.NOT_FOUND_PATH);
+ errorPages[1] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, Constants.SERVER_ERROR_PATH);
registry.addErrorPages(errorPages);
}
diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/config/FilterConfig.java b/novel-front/src/main/java/xyz/zinglizingli/common/config/FilterConfig.java
index 65cc8f9..8654492 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/common/config/FilterConfig.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/common/config/FilterConfig.java
@@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import xyz.zinglizingli.common.filter.SearchFilter;
+import xyz.zinglizingli.common.filter.BookFilter;
@Configuration
public class FilterConfig{
@@ -13,9 +13,9 @@ public class FilterConfig{
private String picSavePath;
@Bean
- public FilterRegistrationBean filterRegist() {
- FilterRegistrationBean frBean = new FilterRegistrationBean();
- frBean.setFilter(new SearchFilter());
+ public FilterRegistrationBean filterRegist() {
+ FilterRegistrationBean frBean = new FilterRegistrationBean<>();
+ frBean.setFilter(new BookFilter());
frBean.addUrlPatterns("/*");
frBean.addInitParameter("picSavePath",picSavePath);
return frBean;
diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/config/IndexRecBooksConfig.java b/novel-front/src/main/java/xyz/zinglizingli/common/config/IndexRecBooksConfig.java
index 33088af..855d0d2 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/common/config/IndexRecBooksConfig.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/common/config/IndexRecBooksConfig.java
@@ -6,6 +6,10 @@ import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
+/**
+ * 首页推荐书籍配置
+ * @author XXY
+ * */
@Component
@ConfigurationProperties(prefix = "index")
public class IndexRecBooksConfig {
diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/filter/BookFilter.java b/novel-front/src/main/java/xyz/zinglizingli/common/filter/BookFilter.java
new file mode 100644
index 0000000..ca991bf
--- /dev/null
+++ b/novel-front/src/main/java/xyz/zinglizingli/common/filter/BookFilter.java
@@ -0,0 +1,222 @@
+package xyz.zinglizingli.common.filter;
+
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.Charsets;
+import org.springframework.core.io.Resource;
+import org.springframework.http.*;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
+import xyz.zinglizingli.common.utils.Constants;
+import xyz.zinglizingli.common.utils.RestTemplateUtil;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.*;
+
+/**
+ * 书籍内容过滤器
+ * @author xxy
+ */
+@Slf4j
+public class BookFilter implements Filter {
+
+
+ /**
+ * 图片后缀集合
+ * */
+ private final List PIC_POSTFIX_LIST = Arrays.asList("jpg","pcx","emf","gif","bmp","tga","jpeg","tif","png","rle");
+
+ /**
+ * 本地图片保存路径前缀
+ * */
+ private String picSavePath;
+
+
+ @Override
+ public void init(FilterConfig filterConfig){
+ picSavePath = filterConfig.getInitParameter("picSavePath");
+ }
+
+ @SneakyThrows
+ @Override
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain){
+ HttpServletRequest req = (HttpServletRequest) servletRequest;
+ HttpServletResponse resp = (HttpServletResponse) servletResponse;
+ String requestUrl = req.getRequestURL().toString();
+ String requestUri = req.getRequestURI();
+
+ String forObject;
+
+ try {
+
+ //本地图片
+ if (requestUri.contains(Constants.LOCAL_PIC_PREFIX)) {
+ OutputStream out = resp.getOutputStream();
+ InputStream input = new FileInputStream(new File(picSavePath + requestUri));
+ byte[] b = new byte[4096];
+ for (int n; (n = input.read(b)) != -1; ) {
+ out.write(b, 0, n);
+ }
+ input.close();
+ out.close();
+ return;
+
+ }
+
+ //非漫画访问
+ if (!requestUrl.contains(Constants.CARTOON_CONTENT_PREFIX)) {
+ filterChain.doFilter(req, resp);
+ return;
+ }
+
+
+ String method = req.getMethod();
+
+ //请求html/css/js等文件
+ RestTemplate restTemplate = RestTemplateUtil.getInstance(Charsets.UTF_8);
+
+ resp.setCharacterEncoding("utf-8");
+
+ String realPath = Constants.CRAWL_CARTOON_URL_PREFIX + requestUri.substring(Constants.CARTOON_CONTENT_PREFIX.length());
+
+
+ if (HttpMethod.GET.name().equals(method)) {
+ //漫画GET请求
+ String postFix = requestUri.substring(requestUri.lastIndexOf(".") + 1);
+ if (PIC_POSTFIX_LIST.contains(postFix)) {
+ // 请求图片
+ restTemplate = RestTemplateUtil.getInstance(Charsets.ISO_8859_1);
+ resp.setContentType("image/apng");
+ }
+ if (requestUri.startsWith(Constants.CARTOON_PIC_PREFIX)) {
+ //漫画图片处理
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Referer", Constants.CRAWL_CARTOON_URL_PREFIX);
+ HttpEntity requestEntity = new HttpEntity<>(null, headers);
+ realPath = Constants.CRAWL_CARTOON_PIC_URL_PREFIX + requestUri.substring(Constants.CARTOON_PIC_PREFIX.length());
+ ResponseEntity resEntity = restTemplate.exchange(realPath, HttpMethod.GET, requestEntity, Resource.class);
+ InputStream input = Objects.requireNonNull(resEntity.getBody()).getInputStream();
+ OutputStream out = resp.getOutputStream();
+ byte[] b = new byte[4096];
+ for (int n; (n = input.read(b)) != -1; ) {
+ out.write(b, 0, n);
+ }
+ input.close();
+ out.close();
+ return;
+
+
+ } else if (requestUri.startsWith(Constants.CARTOON_STATIC_PREFIX)) {
+ //漫画静态文件处理
+ realPath = Constants.CRAWL_CARTOON_STATIC_URL_PREFIX + requestUri.substring(Constants.CARTOON_STATIC_PREFIX.length());
+ ResponseEntity forEntity = restTemplate.getForEntity(realPath, String.class);
+ forObject = forEntity.getBody();
+ assert forObject != null;
+ forObject = forObject.replaceAll(Constants.CRAWL_CARTOON_PIC_URL_PREFIX, Constants.CARTOON_PIC_PREFIX);
+
+ } else {
+ //其他漫画内容处理
+ ResponseEntity forEntity = restTemplate.getForEntity(realPath, String.class);
+ forObject = forEntity.getBody();
+
+ assert forObject != null;
+
+ //漫画内容过滤
+ forObject = forObject.replace(Constants.CRAWL_CARTOON_URL_PREFIX+"js/ad/ad_12.js", "")
+ .replace(Constants.CRAWL_CARTOON_URL_PREFIX+"js/ad/ad_13.js", "")
+ .replace(Constants.CRAWL_CARTOON_STATIC_URL_PREFIX+"ocomic/js/dmzjMhFinally-new.js", "")
+ .replace(Constants.CRAWL_CARTOON_STATIC_URL_PREFIX+"ocomic/js/dmzjMhFinally-new.js", "")
+ .replace(Constants.CRAWL_CARTOON_STATIC_URL_PREFIX+"module/js/float_code.js", "")
+ .replaceAll(Constants.CRAWL_CARTOON_STATIC_URL_PREFIX+"public/js/dmzj-land-2015.6.js", "")
+ .replaceAll("", "")
+ .replaceAll("globalNav.js", "")
+ .replaceAll("TSB.js", "")
+ .replaceAll(Constants.CRAWL_CARTOON_PIC_URL_PREFIX, Constants.CARTOON_PIC_PREFIX)
+ .replaceAll(Constants.CRAWL_CARTOON_STATIC_URL_PREFIX, Constants.CARTOON_STATIC_PREFIX)
+ + ""
+ + "";
+ }
+
+
+ } else {
+ //漫画POST请求
+
+
+ Map oldParameterMap = req.getParameterMap();
+ Set> entries = oldParameterMap.entrySet();
+ Map newParameterMap = new HashMap<>(entries.size());
+ for (Map.Entry entry : entries) {
+ newParameterMap.put(entry.getKey(), entry.getValue()[0]);
+ }
+
+ MultiValueMap map = new LinkedMultiValueMap<>();
+ map.setAll(newParameterMap);
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+ HttpEntity> request = new HttpEntity<>(map, headers);
+ forObject = restTemplate.postForEntity(realPath, request, String.class).getBody();
+ assert forObject != null;
+ forObject = forObject.replaceAll(Constants.CARTOON_CONTENT_PREFIX, Constants.CRAWL_CARTOON_URL_PREFIX)
+ ;
+
+ }
+
+
+ } catch (RuntimeException e) {
+ log.error(e.getMessage(), e);
+ if (e instanceof HttpClientErrorException && (((HttpClientErrorException) e).getStatusCode() == HttpStatus.NOT_FOUND)) {
+ //404
+ resp.sendRedirect(Constants.NOT_FOUND_PATH);
+ return;
+ } else {
+ //500
+ req.getRequestDispatcher(Constants.SERVER_ERROR_PATH).forward(servletRequest, servletResponse);
+ return;
+ }
+
+
+ }
+
+ resp.getWriter().print(forObject);
+ }
+
+
+ @Override
+ public void destroy() {
+
+ }
+
+
+}
diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/filter/SearchFilter.java b/novel-front/src/main/java/xyz/zinglizingli/common/filter/SearchFilter.java
deleted file mode 100644
index 0cee3d1..0000000
--- a/novel-front/src/main/java/xyz/zinglizingli/common/filter/SearchFilter.java
+++ /dev/null
@@ -1,541 +0,0 @@
-package xyz.zinglizingli.common.filter;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.core.io.Resource;
-import org.springframework.http.*;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-import org.springframework.web.client.HttpClientErrorException;
-import org.springframework.web.client.RestTemplate;
-import xyz.zinglizingli.common.cache.CommonCacheUtil;
-import xyz.zinglizingli.common.utils.RestTemplateUtil;
-import xyz.zinglizingli.common.utils.SpringUtil;
-
-import javax.servlet.*;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.*;
-import java.net.URLDecoder;
-import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class SearchFilter implements Filter {
-
- private static final Logger log = LoggerFactory.getLogger(SearchFilter.class);
-
- private CommonCacheUtil cacheUtil;
-
- private static List picPostFix;
-
- private static List localFileFix;
-
- private static List staticFileFix;
-
- private static List noteURI;
-
- private RestTemplate restTemplate;
-
- private String picSavePath;
-
-
-
- private final String SUANWEI_BOOK_REGEX = "";
- private final String SUANWEI_BOOK_HTML_REGEX = "/\\d+_\\d+\\.html";
-
- private final String XIYANGYANG_BOOK_REGEX = "";
- private final String XIYANGYANG_BOOK_HTML_REGEX = "/\\d+_\\d+\\.html";
-
-
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- picSavePath = filterConfig.getInitParameter("picSavePath");
- picPostFix = new ArrayList<>();
- picPostFix.add("jpg");
- picPostFix.add("pcx");
- picPostFix.add("emf");
- picPostFix.add("gif");
- picPostFix.add("bmp");
- picPostFix.add("tga");
- picPostFix.add("jpeg");
- picPostFix.add("tif");
- picPostFix.add("png");
- picPostFix.add("rle");
- localFileFix = new ArrayList<>();
- localFileFix.add("IMG_1470.JPG");
- localFileFix.add("baidu_verify_Ep8xaWQJAI.html");
- localFileFix.add("baidu_verify_L6sR9GjEtg.html");
- localFileFix.add("shenma-site-verification.txt");
- localFileFix.add("favicon.ico");
- localFileFix.add("headerbg.jpg");
- localFileFix.add("mang.png");
- localFileFix.add("HotBook.apk");
- localFileFix.add("wap_collect.js");
- localFileFix.add("note_1.html");
- localFileFix.add("note_2.html");
- localFileFix.add("note_3.html");
- localFileFix.add("note_4.html");
- staticFileFix = new ArrayList<>();
- staticFileFix.add("jpg");
- staticFileFix.add("gif");
- staticFileFix.add("bmp");
- staticFileFix.add("jpeg");
- staticFileFix.add("png");
- staticFileFix.add("js");
- staticFileFix.add("css");
- noteURI = new ArrayList<>();
- noteURI.add("/html/note_1.html");
- noteURI.add("/html/note_2.html");
-
- }
-
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
- String forObject = null;
- HttpServletRequest req = (HttpServletRequest) servletRequest;
- HttpServletResponse resp = (HttpServletResponse) servletResponse;
- String requestURL = req.getRequestURL().toString();
- String requestURI = req.getRequestURI();
-
- try {
-
- if(requestURI.contains("/localPic/")){
- OutputStream out = resp.getOutputStream();
- InputStream input = new FileInputStream(new File(picSavePath+requestURI));
- byte[] b = new byte[4096];
- for (int n; (n = input.read(b)) != -1;) {
- out.write(b, 0, n);
- }
- input.close();
- out.close();
- return;
-
- }
-
- if (!requestURL.contains("/manhua/")) {
- filterChain.doFilter(req, resp);
- return;
- }
-
-
- final String method = req.getMethod();
-
-
- // 案例:充当客户端通过restTemplate请求网络数据,并充当服务端将数据返回给浏览器
- // 客户端请求数据:输入流(byte[])==》字符串
- // 服务端响应数据:字符串 == 》 输出流(byte[])
-
- //默认方式:
- //RestTemplate restTemplate = new RestTemplate();
- // ①当返回的response-header的content-type属性有charset值时,
- // restTemplate的StringHttpMessageConverter会设置默认charset为content-type属性
- // charset值
- // StringHttpMessageConverter.setDefaultCharset(Charset.forName(charset));
- // ②当返回的response-header的content-type属性没有charset值时
- // restTemplate的StringHttpMessageConverter会使用默认的charset即ISO-8859-1
-
- // 对服务端请求返回的输入流(byte[])采用何种编码转换成字符串(String)
- restTemplate = RestTemplateUtil.getInstance("utf-8");//请求html/css/js等文件
- // 对客户端响应返回的字符串(String)采用何种编码转换成输出流(byte[])
- resp.setCharacterEncoding("utf-8");
-
- /*//=====现在浏览器有编码自动识别功能,所以上面的代码没有加content-type的Header也没有问题==========
- //=====正确做法应该是下面代码片段1和代码片段2二选一==========
-
- //===============================================代码片段1===============================
- // 对客户端响应返回的字符串(String)采用何种编码转换成输出流(byte[])
- resp.setCharacterEncoding("utf-8");
- // 告诉浏览器对服务端请求返回的输入流(byte[])采用何种编码转换成字符串(String)显示
- resp.setHeader("content-type", "text/html;charset=utf-8");
- //===============================================代码片段1===============================
-
-
- //===============================================代码片段2===============================
- //对客户端响应返回的字符串(String)采用何种编码转换成输出流(byte[])
- //并且告诉浏览器对服务端请求返回的输入流(byte[])采用何种编码转换成字符串(String)显示
- resp.setContentType("text/html;charset=utf-8");
- //===============================================代码片段2===============================
-*/
-
-
- if (HttpMethod.GET.name().equals(method)) {
-
-
- String fileName = requestURI.substring(requestURI.lastIndexOf("/") + 1);
- if (localFileFix.contains(fileName) || fileName.startsWith("9a4a540e-1759-4268-90fa-7fb652c3604a.")) {
- filterChain.doFilter(servletRequest, servletResponse);
- return;
- }
-
-
- String queryString = req.getQueryString();
- if (queryString != null && queryString.length() > 0 && !queryString.contains("bsh_bid=")) {
- queryString = "?" + URLDecoder.decode(req.getQueryString());
- } else {
- queryString = "";
- }
-
-
- if (forObject == null) {
- String postFix = requestURI.substring(requestURI.lastIndexOf(".") + 1);
- if (picPostFix.contains(postFix)) {
- // 对服务端请求返回的输入流(byte[])采用何种编码转换成字符串(String)
- restTemplate = RestTemplateUtil.getInstance("iso-8859-1");//请求图片
- resp.setContentType("image/apng");
- }
- if (requestURI.startsWith("/manhua/images/")) {
- HttpHeaders headers = new HttpHeaders();
- headers.add("Referer","https://www.dmzj.com");
- HttpEntity requestEntity = new HttpEntity<>(null, headers);
- String realUrl = "https://images.dmzj.com/" + requestURI.substring(15);
- ResponseEntity resEntity = restTemplate.exchange(realUrl, HttpMethod.GET, requestEntity, Resource.class);
- InputStream input = resEntity.getBody().getInputStream();
- OutputStream out = resp.getOutputStream();
- byte[] b = new byte[4096];
- for (int n; (n = input.read(b)) != -1;) {
- out.write(b, 0, n);
- }
- input.close();
- out.close();
- return;
-
-
- // 对服务端请求返回的输入流(byte[])采用何种编码转换成字符串(String)
-
-
-
-
- } else if(requestURI.startsWith("/manhua/statics/")){
- String realPath = "https://static.dmzj.com/"+requestURI.substring(16);
- ResponseEntity forEntity = restTemplate.getForEntity(realPath, String.class);
- forObject = forEntity.getBody();
- forObject = forObject.replaceAll("https://images.dmzj.com/", "/manhua/images/");
-
- }else {
- String realPath = "https://www.dmzj.com/" + requestURI.substring(8);
-
- ResponseEntity forEntity = restTemplate.getForEntity(realPath, String.class);
- forObject = forEntity.getBody();
-
- // forObject = new String(forObject.getBytes("ISO-8859-1"),"utf-8");
- forObject = forObject.replace("https://www.dmzj.com/js/ad/ad_12.js", "");
- forObject = forObject.replace("https://www.dmzj.com/js/ad/ad_13.js", "");
- forObject = forObject.replace("https://static.dmzj.com/ocomic/js/dmzjMhFinally-new.js", "");
- forObject = forObject.replace("https://static.dmzj.com/ocomic/js/dmzjMhFinally-new.js", "");
- forObject = forObject.replace("https://static.dmzj.com/module/js/float_code.js", "");
- forObject = forObject.replaceAll("", "");
- forObject = forObject.replaceAll("https://static.dmzj.com/public/js/dmzj-land-2015.6.js", "");
- forObject = forObject.replaceAll("", "");
- forObject = forObject.replaceAll("", "");
- forObject = forObject.replaceAll("", "");
- forObject = forObject.replaceAll("globalNav.js", "");
- forObject = forObject.replaceAll("TSB.js", "");
- forObject = forObject.replaceAll("https://images.dmzj.com/", "/manhua/images/");
- forObject = forObject.replaceAll("https://static.dmzj.com/", "/manhua/statics/");
- forObject = forObject + "";
- forObject = forObject + "";
-// forObject = forObject.replaceAll("/manhua/", "https://www.dmzj.com/")
-// .replaceAll("笔趣岛", "酸味书屋")
-// .replaceAll("笔趣阁", "酸味书屋")
-// .replaceAll("class=\"mainNav independNav\"", "style='dispaly:none' class=\"mainNav independNav\"")
-// .replaceAll("", "")
-// .replaceFirst("", "")
-// .replaceAll("", "")
-// .replaceAll("https://zhannei.baidu.com/cse", "http://m.zinglizingli.xyz")
-// .replaceAll("返回", "返回")
-// .replaceAll("加入书架", "加入收藏")
-// .replaceFirst("", "\n")//页面访问自动推送到百度
-// .replaceAll("", "")//去除广告
- ;
-
-
-// if (requestURI.matches(SUANWEI_BOOK_HTML_REGEX)) {
-// Pattern pattern = Pattern.compile("(.+)\\s+目录共\\d+章
");
-// Matcher matcher = pattern.matcher(forObject);
-// String title = "";
-// if (matcher.find()) {
-// title = matcher.group(1);
-// }// 类别:武侠仙侠
-// pattern = Pattern.compile("作者:(.+)
");
-// matcher = pattern.matcher(forObject);
-// String author = "";
-// if (matcher.find()) {
-// author = matcher.group(1);
-// }
-// pattern = Pattern.compile("(.+)");
-// matcher = pattern.matcher(forObject);
-// String sort = "";
-// if (matcher.find()) {
-// sort = matcher.group(1);
-// }
-// String desc = title + "," + title + "小说最新章节免费在线阅读、最新章节列表," + title + "小说最新更新免费提供,《" + title + "》是一本情节与文笔俱佳的" + sort + "小说,由作者" + author + "创建。";
-//
-// forObject = forObject.replaceFirst("]+\"\\s*/?>", "");//[^>]+表示1个或多个不是>的字符
-// forObject = forObject.replaceFirst("", "");
-//
-//
-// }
-//
-// if ("/".equals(requestURI)) {
-// forObject = forObject.replaceFirst("]+\"\\s*/?>", "");//[^>]+表示1个或多个不是>的字符
-// forObject = forObject.replaceFirst("", "");
-//
-//
-// }
- }
- }
-
-
- } else {
-
-
- Map oldParameterMap = req.getParameterMap();
- Map newParameterMap = new HashMap<>();
- Set> entries = oldParameterMap.entrySet();
- for (Map.Entry entry : entries) {
- newParameterMap.put(entry.getKey(), entry.getValue()[0]);
- }
-
- MultiValueMap map = new LinkedMultiValueMap<>();
- map.setAll(newParameterMap);
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- HttpEntity> request = new HttpEntity<>(map, headers);
- forObject = restTemplate.postForEntity("https://www.dmzj.com/" + requestURI.substring(8), request, String.class).getBody();
- // forObject = new String(forObject.getBytes("ISO-8859-1"),"utf-8");
- forObject = forObject.replaceAll("/manhua/", "https://www.dmzj.com/")
- .replaceAll("笔趣岛", "酸味书屋")
- .replaceAll("笔趣阁", "酸味书屋")
- .replaceFirst("", "")
- .replaceAll("书架", "笔记")
- .replaceAll("返回", "返回")
- ;
- forObject = setBookURIToHTML(forObject, SUANWEI_BOOK_REGEX);
- //resp.setCharacterEncoding("utf-8");
- //setContentType(postFix, resp);
-
- }
-
-
- } catch (RuntimeException e) {
- log.error(e.getMessage(), e);
- if (e instanceof HttpClientErrorException && (((HttpClientErrorException) e).getStatusCode() == HttpStatus.NOT_FOUND)) {
- //404
- resp.sendRedirect("/");
- return;
- } else {
- req.getRequestDispatcher("/mang.html").forward(servletRequest, servletResponse);
- return;
- }
-
-
- //resp.setCharacterEncoding("utf-8");
-
- }
-
- resp.getWriter().print(forObject);
- return;
- }
-
- private String addAttacDivForSearch(String forObject, String requestURI) {
- try {
- if (requestURI.endsWith(".html") || requestURI.equals("/")) {
- String hotNewsDiv = cacheUtil.get("hotNewsDiv");
- if (hotNewsDiv == null) {
- MultiValueMap mmap = new LinkedMultiValueMap<>();
- HttpHeaders headers = new HttpHeaders();
- headers.add("Host", "channel.chinanews.com");
- headers.add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
- HttpEntity> request = new HttpEntity<>(mmap, headers);
- String body = restTemplate.postForEntity("http://channel.chinanews.com/cns/cjs/sh.shtml", request, String.class).getBody();
- Pattern pattern = Pattern.compile("specialcnsdata\\s*=\\s*\\{\"docs\":(.+)};\\s+newslist\\s*=\\s*specialcnsdata;");
- Matcher matcher = pattern.matcher(body);
- if (matcher.find()) {
- String jsonResult = matcher.group(1);
- if (jsonResult.length() > 5) {
- List