后段的html文本,去掉不必要的空格和换行,进行压缩
引入pom
<!-- https://mvnrepository.com/artifact/com.googlecode.htmlcompressor/htmlcompressor -->
<dependency>
<groupId>com.googlecode.htmlcompressor</groupId>
<artifactId>htmlcompressor</artifactId>
<version>1.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.yahoo.platform.yui/yuicompressor -->
<dependency>
<groupId>com.yahoo.platform.yui</groupId>
<artifactId>yuicompressor</artifactId>
<version>2.4.8</version>
</dependency>
test Demo
@Test
public void htmlCompress() {
HtmlCompressor htmlCompressor = new HtmlCompressor();
htmlCompressor.setCompressCss(true);
htmlCompressor.setCompressJavaScript(true);
htmlCompressor.setRemoveComments(true);
String htmlStr = "<p>我爱你 <span>ha ha</span> </p> <p>h</p> <div> asd </div>";
Document document = Jsoup.parse(htmlStr);
String html = document.body().html();
System.out.println(html);
System.out.println(document.body().wholeText());
System.out.println(htmlCompressor.compress(html));
}