问题
在普通Java代码中输出HTML时,是否有推荐的方法来转义<
,>
,"
和&
字符? (除了手动执行以下操作外,即)。
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = source.replace("<", "<").replace("&", "&"); // ...
#1 热门回答(234 赞)
StringEscapeUtilsfromApache Commons Lang:
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
// ...
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = escapeHtml(source);
Forversion 3:
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
// ...
String escaped = escapeHtml4(source);
#2 热门回答(112 赞)
Apache Commons的替代方案:UseSpring'sHtmlUtils.htmlEscape(String input)
方法。
#3 热门回答(49 赞)
好短的方法:
public static String escapeHTML(String s) {
StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
out.append("&#");
out.append((int) c);
out.append(';');
} else {
out.append(c);
}
}
return out.toString();
}
基于https://stackoverflow.com/a/8838023/1199155(放大器在那里丢失)。根据http://www.w3.org/TR/html4/sgml/entities.html,在if子句中检查的四个字符是低于128的唯一字符