第一种,会对中文也进行转码:
StringEscapeUtils.escapeHtml
第二种,不会对中文转码:
HtmlUtils.htmlEscape
第三种:
public static String htmlEncode(String source) { if (source == null) { return ""; } String html = ""; StringBuffer buffer = new StringBuffer(); for (int i = 0; i < source.length(); i++) { char c = source.charAt(i); switch (c) { case '<': buffer.append("<"); break; case '>': buffer.append(">"); break; case '&': buffer.append("&"); break; case '"': buffer.append("""); break; case 10: case 13: break; default: buffer.append(c); } } html = buffer.toString(); return html; }