1、引入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
2、编写Java代码
import org.apache.commons.text.StringEscapeUtils;
@Test
void TestEscape() {
String htmlStr = "<script>hello</script>";
String escapeStr StringEscapeUtils.escapeHtml4(htmlStr);
System.out.println(escapeStr);
}