#第一步:引入Jsoup和lang和lang3的依赖:
Jsoup是HTML解析器lang和lang3这两个包里有转换所需的工具类
< groupId > org.jsoup</ groupId>
< artifactId > jsoup</ artifactId>
< version > 1.11 .3 < / version >
< / dependency >
< dependency >
< groupId > commons - lang</ groupId>
< artifactId > commons - lang</ artifactId>
< version > 2.6 < / version >
< / dependency >
< dependency >
< groupId > org.apache.commons</ groupId>
< artifactId > commons - lang3</ artifactId>
< version > 3.4 < / version >
< / dependency>
#第二步:直接使用即可:
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
/**
* @author Piconjo
*/
public class Html2PlainText {
public static String convert( String html )
{
if ( StringUtils.isEmpty( html ) )
{
return("");
}
Document document = Jsoup.parse( html );
Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint( false );
document.outputSettings( outputSettings );
document.select( "br" ).append( "\\n" );
document.select( "p" ).prepend( "\\n" );
document.select( "p" ).append( "\\n" );
String newHtml = document.html().replaceAll( "\\\\n", "\n" );
String plainText = Jsoup.clean( newHtml, "", Whitelist.none(), outputSettings );
String result = StringEscapeUtils.unescapeHtml( plainText.trim() );
return(result);
}
}
#使用测试:

