本示例演示Spring Boot 1.5.9.RELEASE版本的用户手册下载
pom.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.nihaorz</groupId> 8 <artifactId>spring-boot-doc-download</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> 13 <dependency> 14 <groupId>org.jsoup</groupId> 15 <artifactId>jsoup</artifactId> 16 <version>1.11.2</version> 17 </dependency> 18 <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> 19 <dependency> 20 <groupId>commons-io</groupId> 21 <artifactId>commons-io</artifactId> 22 <version>2.6</version> 23 </dependency> 24 </dependencies> 25 26</project>
Application.java
1package com.nihaorz; 2 3import org.apache.commons.io.FileUtils; 4import org.apache.commons.io.IOUtils; 5import org.jsoup.Jsoup; 6import org.jsoup.nodes.Document; 7import org.jsoup.nodes.Element; 8import org.jsoup.select.Elements; 9 10import java.io.File; 11import java.io.IOException; 12import java.net.URL; 13import java.nio.charset.Charset; 14 15/** 16 * @author Nihaorz 17 */ 18public class Application { 19 20 private static final Charset CHARSET = Charset.forName("UTF-8"); 21 22 /** 23 * doc站点地址 24 */ 25 private static final String DOC_URL = "https://docs.spring.io/spring-boot/docs/"; 26 27 /** 28 * doc起始页 29 */ 30 private static final String HTML_URI = "/reference/html/"; 31 32 /** 33 * URL分隔符 34 */ 35 private static final String URL_SEPARATOR = "/"; 36 37 /** 38 * html页面结束标记 39 */ 40 private static final String HTML_END_STR = ".html"; 41 42 public static void main(String[] args) throws IOException { 43 String storePath = "D:\\IDEA_WS\\OperationCenter_WS\\spring-boot-doc-download\\src\\main\\doc"; 44 String version = "1.5.9.RELEASE"; 45 generateHtmlDoc(storePath, version); 46 } 47 48 /** 49 * 生成文档 50 * 51 * @param storePath 52 * @param version 53 */ 54 private static void generateHtmlDoc(String storePath, String version) throws IOException { 55 String indexPath = DOC_URL + version + HTML_URI; 56 String html = IOUtils.toString(new URL(indexPath), CHARSET); 57 saveHtml(storePath, indexPath, html); 58 Document document = Jsoup.parse(html); 59 Elements elements = new Elements(); 60 elements.addAll(document.select("span.chapter>a")); 61 elements.addAll(document.select("span.part>a")); 62 elements.addAll(document.select("span.appendix>a")); 63 String urlPath; 64 System.out.println("共解析得到" + elements.size() + "个子页面"); 65 for (Element element : elements) { 66 urlPath = element.attr("href"); 67 if (urlPath != null && !"".equals(urlPath.trim())) { 68 if (!urlPath.startsWith("http")) { 69 urlPath = indexPath + urlPath; 70 } 71 } 72 html = IOUtils.toString(new URL(urlPath), CHARSET); 73 saveHtml(storePath, urlPath, html); 74 } 75 } 76 77 private static void saveHtml(String storePath, String urlPath, String html) throws IOException { 78 if (!urlPath.contains(".html#")) { 79 if (!urlPath.endsWith(HTML_END_STR)) { 80 if (urlPath.endsWith(URL_SEPARATOR)) { 81 urlPath = urlPath + "index" + HTML_END_STR; 82 } else { 83 urlPath = urlPath + URL_SEPARATOR + "index" + HTML_END_STR; 84 } 85 } 86 urlPath = urlPath.substring(DOC_URL.length(), urlPath.length()); 87 String localPath = storePath + File.separator + urlPath.replace(URL_SEPARATOR, File.separator); 88 File file = new File(localPath); 89 if (!file.exists()) { 90 file.getParentFile().mkdirs(); 91 } 92 file.createNewFile(); 93 FileUtils.write(file, html, CHARSET); 94 System.out.println("保存" + file.getAbsolutePath() + "成功"); 95 } 96 } 97 98}
运行main函数会在D:\IDEA_WS\OperationCenter_WS\spring-boot-doc-download\src\main\doc\1.5.9.RELEASE\reference\html目录下生成html文件
将以下脚本批量为空字符串
<script>if(window.parent==window){(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create','UA-2728886-23','auto',{'siteSpeedSampleRate':100});ga('send','pageview');}</script>
然后将html文件放到tomcat ROOT应用下访问index.html,使用Fiddler抓包会有如下几个文件404:
highlight.css
manual.css
manual-multipage.css
background.png
important.png
note.png
tip.png
warning.png
email-decode.min.js
他们的真是下载地址如下:
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/css/highlight.css
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/css/manual.css
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/css/manual-multipage.css
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/images/background.png
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/images/important.png
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/images/note.png
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/images/tip.png
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/images/warning.png
https://docs.spring.io/cdn-cgi/scripts/d07b1474/cloudflare-static/email-decode.min.js
将上面的文件下载下来依次放到html下的css、images、js目录下
在上面的html文件中全局搜索email-decode.min.js并替换为相对路径,然后一个新鲜离线版用户手册就诞生了,从tomcat中拿出来邮件浏览器打开即可使用,还不带google统计,跟官方在线版本简直一毛一样啊。
下面放大招