Java 中 HTML 转义与反转义工具类
代码如下
import org.apache.commons.lang3.StringUtils;
public class EscapeUtil {
private static final char[][] TEXT = new char[64][];
static {
for (int i = 0; i < 64; i++) {
TEXT[i] = new char[]{(char) i};
}
// 单引号
TEXT['\''] = "'".toCharArray();
// 单引号
TEXT['"'] = """.toCharArray();
// &符
TEXT['&'] = "&".toCharArray();
// 小于号
TEXT['<'] = "<".toCharArray();
// 大于号
TEXT['>'] = ">".toCharArray();
}
public static String escape(String text) {
return encode(text);
}
public static String unescape(String content) {
return decode(content);
}
public static String clean(String content) {
return new HTMLFilter().filter(content);
}
private static String encode(String text) {
int len;
if ((text == null) || ((len = text.length()) == 0)) {
return "";
}
StringBuilder buffer = new StringBuilder(len + (len >> 2));
char c;
for (int i = 0; i < len; i++) {
c = text.charAt(i);
if (c < 64) {
buffer.append(TEXT[c]);
} else {
buffer.append(c);
}
}
return buffer.toString();
}
public static String decode(String content) {
if (StringUtils.isEmpty(content)) {
return content;
}
StringBuilder tmp = new StringBuilder(content.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < content.length()) {
pos = content.indexOf("%", lastPos);
if (pos == lastPos) {
if (content.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(content.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(content.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(content.substring(lastPos));
lastPos = content.length();
} else {
tmp.append(content.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
}
测试用例
public static void main(String[] args) {
String html = "<script>alert(1);</script>";
System.out.println(EscapeUtil.clean(html));
System.out.println(EscapeUtil.escape(html));
System.out.println(EscapeUtil.unescape(html));
}
输出:
alert(1);
<script>alert(1);</script>
<script>alert(1);</script>
如您在阅读中发现不足,欢迎留言!!!