如何在 Java 中转义 HTML 特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1224996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:07:23  来源:igfitidea点击:

How to escape HTML special characters in Java?

javahtml

提问by Nathaniel Flath

Is there a way to convert a string to a string that will display properly in a web document? For example, changing the string

有没有办法将字符串转换为可以在 Web 文档中正确显示的字符串?例如,更改字符串

"<Hello>"

To

"&lt;Hello&gt;"

回答by Laurence Gonsalves

That's usually called "HTML escaping". I'm not aware of anything in the standard libraries for doing this (though you can approximate it by using XML escaping). There are lots of third-party libraries that can do this, however. StringEscapeUtilsfrom org.apache.commons.lang has a escapeHtmlmethod that can do this.

这通常称为“HTML 转义”。我不知道标准库中的任何内容(尽管您可以通过使用 XML 转义来近似它)。然而,有很多第三方库可以做到这一点。org.apache.commons.lang 中的StringEscapeUtils有一个escapeHtml方法可以做到这一点。

回答by Sorantis

public static String stringToHTMLString(String string) {
    StringBuffer sb = new StringBuffer(string.length());
    // true if last char was blank
    boolean lastWasBlankChar = false;
    int len = string.length();
    char c;

    for (int i = 0; i < len; i++)
        {
        c = string.charAt(i);
        if (c == ' ') {
            // blank gets extra work,
            // this solves the problem you get if you replace all
            // blanks with &nbsp;, if you do that you loss 
            // word breaking
            if (lastWasBlankChar) {
                lastWasBlankChar = false;
                sb.append("&nbsp;");
                }
            else {
                lastWasBlankChar = true;
                sb.append(' ');
                }
            }
        else {
            lastWasBlankChar = false;
            //
            // HTML Special Chars
            if (c == '"')
                sb.append("&quot;");
            else if (c == '&')
                sb.append("&amp;");
            else if (c == '<')
                sb.append("&lt;");
            else if (c == '>')
                sb.append("&gt;");
            else if (c == '\n')
                // Handle Newline
                sb.append("&lt;br/&gt;");
            else {
                int ci = 0xffff & c;
                if (ci < 160 )
                    // nothing special only 7 Bit
                    sb.append(c);
                else {
                    // Not 7 Bit use the unicode system
                    sb.append("&#");
                    sb.append(new Integer(ci).toString());
                    sb.append(';');
                    }
                }
            }
        }
    return sb.toString();
}

回答by Borislav Gizdov

HTMLEntities is an Open Source Java class that contains a collection of static methods (htmlentities, unhtmlentities, ...) to convert special and extended characters into HTML entitities and vice versa.

HTMLEntities 是一个开源 Java 类,它包含一组静态方法(htmlentities、unhtmlentities 等),用于将特殊字符和扩展字符转换为 HTML 实体,反之亦然。

http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=htmlentities

http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=htmlentities

回答by Plcode

Better do it yourself, if you know the logic behind - it is easy:

最好自己做,如果你知道背后的逻辑 - 这很容易:

 public class ConvertToHTMLcode {
        public static void main(String[] args) throws IOException {
          String specialSymbols = "?%? Stra?e";
          System.out.println(convertToHTMLCodes(specialSymbols)); //&#7877;%&#223;
   }

   public static String convertToHTMLCodes(String str) throws IOException {
      StringBuilder sb = new StringBuilder();
      int len = str.length();
      for(int i = 0; i < len; ++i) {
          char c = str.charAt(i);
         if (c > 127) {
            sb.append("&#");
            sb.append(Integer.toString(c, 10));
            sb.append(";");
        } else {
            sb.append(c);
        }
     }
       return sb.toString();
   }
}