public class HtmlUtil extends Object
比如去掉指定标签(例如广告栏等)、去除JS、去掉样式等等,这些操作都可以使用此工具类完成。
Modifier and Type | Field and Description |
---|---|
static Pattern |
META_CHARSET_PATTERN
正则:匹配meta标签的编码信息
|
static Pattern |
RE_HTML_MARK
HTML标签正则
|
static Pattern |
RE_SCRIPT
script标签正则
|
Constructor and Description |
---|
HtmlUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
cleanHtmlTag(String content)
清除所有HTML标签,但是不删除标签内的内容
|
static String |
escape(String text)
转义文本中的HTML字符为安全的字符,以下字符被转义:
' 替换为 ' (' doesn't work in HTML4)
" 替换为 "
& 替换为 &
< 替换为 <
> 替换为 >
|
static String |
filter(String htmlContent)
过滤HTML文本,防止XSS攻击
|
static String |
getString(byte[] contentBytes,
Charset charset,
boolean isGetCharsetFromContent)
从流中读取内容
首先尝试使用charset编码读取内容(如果为空默认UTF-8),如果isGetCharsetFromContent为true,则通过正则在正文中获取编码信息,转换为指定编码; |
static String |
getString(InputStream in,
Charset charset,
boolean isGetCharsetFromContent)
从流中读取内容
首先尝试使用charset编码读取内容(如果为空默认UTF-8),如果isGetCharsetFromContent为true,则通过正则在正文中获取编码信息,转换为指定编码; |
static String |
removeAllHtmlAttr(String content,
String... tagNames)
去除指定标签的所有属性
|
static String |
removeHtmlAttr(String content,
String... attrs)
去除HTML标签中的属性,如果多个标签有相同属性,都去除
|
static String |
removeHtmlTag(String content,
boolean withTagContent,
String... tagNames)
清除指定HTML标签
不区分大小写 |
static String |
removeHtmlTag(String content,
String... tagNames)
清除指定HTML标签和被标签包围的内容
不区分大小写 |
static String |
removeScriptTag(String content)
清除所有script标签,包括内容
|
static String |
unescape(String htmlStr)
还原被转义的HTML特殊字符
|
static String |
unwrapHtmlTag(String content,
String... tagNames)
清除指定HTML标签,不包括内容
不区分大小写 |
public static final Pattern RE_HTML_MARK
public static final Pattern RE_SCRIPT
public static final Pattern META_CHARSET_PATTERN
public static String escape(String text)
text
- 被转义的文本public static String unescape(String htmlStr)
htmlStr
- 包含转义符的HTML内容public static String cleanHtmlTag(String content)
content
- 文本public static String removeScriptTag(String content)
content
- 文本public static String removeHtmlTag(String content, String... tagNames)
content
- 文本tagNames
- 要清除的标签public static String unwrapHtmlTag(String content, String... tagNames)
content
- 文本tagNames
- 要清除的标签public static String removeHtmlTag(String content, boolean withTagContent, String... tagNames)
content
- 文本withTagContent
- 是否去掉被包含在标签中的内容tagNames
- 要清除的标签public static String removeHtmlAttr(String content, String... attrs)
content
- 文本attrs
- 属性名(不区分大小写)public static String removeAllHtmlAttr(String content, String... tagNames)
content
- 内容tagNames
- 指定标签public static String filter(String htmlContent)
htmlContent
- HTML内容public static String getString(InputStream in, Charset charset, boolean isGetCharsetFromContent)
in
- 输入流charset
- 字符集isGetCharsetFromContent
- 是否从返回内容中获得编码信息public static String getString(byte[] contentBytes, Charset charset, boolean isGetCharsetFromContent)
contentBytes
- 内容byte数组charset
- 字符集isGetCharsetFromContent
- 是否从返回内容中获得编码信息Copyright © 2025. All rights reserved.