• JEditorPane中html文档中文乱码解决


    • Culturally dependent information in some documents is handled through a mechanism called character encoding. Character encoding is an unambiguous mapping of the members of a character set (letters, ideographs, digits, symbols, or control functions) to specific numeric code values. It represents the way the file is stored. Example character encodings are ISO-8859-1, ISO-8859-5, Shift-jis, Euc-jp, and UTF-8. When the file is passed to an user agent (JEditorPane) it is converted to the document character set (ISO-10646 aka Unicode).

      There are multiple ways to get a character set mapping to happen with JEditorPane.

      1. One way is to specify the character set as a parameter of the MIME type. This will be established by a call to the setContentType method. If the content is loaded by thesetPage method the content type will have been set according to the specification of the URL. It the file is loaded directly, the content type would be expected to have been set prior to loading.
      2. Another way the character set can be specified is in the document itself. This requires reading the document prior to determining the character set that is desired. To handle this, it is expected that the EditorKit.read operation throw a ChangedCharSetException which will be caught. The read is then restarted with a new Reader that uses the character set specified in the ChangedCharSetException (which is an IOException).
    • setContentType

      public final void setContentType(String type)
      Sets the type of content that this editor handles. This calls getEditorKitForContentType, and then setEditorKit if an editor kit can be successfully located. This is mostly convenience method that can be used as an alternative to calling setEditorKit directly.

      If there is a charset definition specified as a parameter of the content type specification, it will be used when loading input streams using the associated EditorKit. For example if the type is specified as text/html; charset=EUC-JP the content will be loaded using the EditorKit registered for text/html and the Reader provided to theEditorKit to load unicode into the document will use the EUC-JP charset for translating to unicode. If the type is not recognized, the content will be loaded using theEditorKit registered for plain text, text/plain.

      Parameters:
      type - the non-null mime type for the content editing support
      Throws:
      NullPointerException - if the type parameter is null
      See Also:
      getContentType()
      所以总之就是调用setContentType("text/html;charset=utf-8");即可。
       
  • 相关阅读:
    SpringMVC详解
    会话控制cookie和session
    jsp详解
    Jquery Enter事件
    JS获取request字符串
    net 2.0使用ajax
    C#通过WebClient/HttpWebRequest实现http的post/get方法
    WCF Rest:不使用UriTemplate使用post方式传参解决HTTP400问题以及参数映射问题
    关于ASP.NET 中站点地图sitemap 的使用
    Web.config自定义节点configSections
  • 原文地址:https://www.cnblogs.com/xby1993/p/3170196.html
Copyright © 2020-2023  润新知