您的位置:首页 > Web前端 > HTML

过滤html恶意代码

2016-06-30 15:58 405 查看
/**
* 转义HTML特殊字符
*/
public static final String trunhtml(String html){
if(html == null)
return null;
final StringBuilder newhtml = new StringBuilder("");
final char[] chararray = html.toCharArray();
for(char c : chararray){
if(c == '\r')
continue;
if(c == '&')
newhtml.append("&");
else if(c == '#')
newhtml.append("#");
else if(c == '*')
newhtml.append("*");
else if(c == ':')
newhtml.append(":");
else if(c == ';')
newhtml.append(";");
else if(c == '<')
newhtml.append("<");
else if(c == '>')
newhtml.append(">");
else if(c == ' ')
newhtml.append(" ");
else if(c == '\n')
newhtml.append("<br />");
else if(c == '"')
newhtml.append(""");
else if(c == '\'')
newhtml.append("'");
else if(c == '/')
newhtml.append("/");
else if(c == '$')
newhtml.append("$");
else if(c == '(')
newhtml.append("(");
else if(c == ')')
newhtml.append(")");
else if(c == '{')
newhtml.append("{");
else if(c == '}')
newhtml.append("}");
else if(c == '*')
newhtml.append("*");
else if(c == '%')
newhtml.append("%");
else if(c == '+')
newhtml.append("+");
else if(c == '-')
newhtml.append("-");
else if(c == '~')
newhtml.append("~");
else if(c == '\t')
newhtml.append("    ");
else
newhtml.append(c);
}
return newhtml.toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: