• 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");即可。
     

JEditorPane中html文档中文乱码解决的更多相关文章

  1. 解决Asp.net Core中chtml文档中文乱码的问题

    原因 由于Visual Studio 2017在保存chtml时,文本格式非utf-8,所以导致中文会出现乱码情况. 解决办法 在工具->扩展与更新中添加插件"ForceUTF8 (w ...

  2. sublime text3 中 python3编译出现中文乱码解决方法

    转 https://blog.csdn.net/Bin_bin_bingo/article/details/80531582 一.乱码现象 利用sublime自带编译快捷方式ctrl+B会出现中文乱码 ...

  3. Linux中文件/文本的中文乱码解决方法

    Linux显示在Windows编辑过的中文就会显示乱码是由于两个操作系统使用的编码不同所致.Linux下使用的编码是utf8,而Windows使用的是gb18030.因此,解决Linux打开txt/c ...

  4. linux中使用unzip命令中文乱码解决办法

    今天在使用unzip进行解压缩文件时,发现解压出的文件中文乱码,最后使用如下命令解决: unzip -O CP936 xxx.zip 特此记录一下.

  5. url中向后台传递中文乱码解决方法

    方法一: 1.jsp中代码 var userNo = $('#prisoner_id').val();      userNo = encodeURI(userNo);      allPrisone ...

  6. 【转】IDEA 中配置文件properties文件中文乱码解决

    1.首先我们的IDEA文件编码一般都修改为utf-8(setting-->file encodings--->Global Encoding 和 Project Encoding 都设置为 ...

  7. 手机打开PDF文档中文英文支持(乱码问题)解决攻略

    电子书的优点很多,随时随地阅读,无论白天黑夜走路坐车都能阅读:想确认一下某句话是不是这本书里的,搜索一下就可以知道:搬家也不用发愁,几万本书带在身上,依然轻松步行.我买了一台平板主要动因就是为了看书, ...

  8. Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法

    本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...

  9. Atom打开txt文件中文乱码解决、指定文件的语法格式、win10中禁止睡眠

    1.Atom中文乱码解决 首先保证打开的txt文件的编码格式为UTF-8无BOM编码格式,可以使用Notepad++更改,如下图所示: 然后再在atom中打开文件,并右键点击文件内容的任意位置,Cha ...

随机推荐

  1. windows 一个进程可以允许最大的线程数

    默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小. 你也 ...

  2. Fragment详解

    1 Fragment简介 1.1 Fragment的设计初衷 Android3.0引入Fragment的初衷是为大屏幕的设备提供更加灵活的动态UI设计,由于大屏设备可以容纳更多的UI组件,且这些UI组 ...

  3. (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)

    题目大意:有N*M的矩阵稻田,'W'表示有积水的地方, '.'表示是干旱的地方,问稻田内一共有多少块积水,根据样例很容易得出,积水是8个方向任一方向相连即可. 题目大意:有N*M的矩阵稻田,'W'表示 ...

  4. <转>使用eclipse编译cocos2d-x示例项目,创建cocos2d-x android项目并部署到真机

    准备 今天将cocos2d-x的示例项目tests编译到android真机运行,以及如何创建cocos2d-x的android项目. 打开cocos2d-x的tests项目,路径为:D:\cocos2 ...

  5. [AngularJS] Taking control of your templates using $templateCache

    Using $templateCache for quickly retrieval from the cache after first time used. $templateCache main ...

  6. JAVA实现的异步redisclient

    再使用redis的过程中,发现使用缓存尽管好,可是有些地方还是比較难权衡,缓存对象大了,存储对象时的序列化工作非常繁重,消耗大量cpu:那么切分成非常小的部分吧,存取的次数变多了,redisclien ...

  7. Linux PAM&&PAM后门

    Linux PAM&&PAM后门 我是壮丁 · 2014/03/24 11:08 0x00 PAM简介 PAM (Pluggable Authentication Modules )是 ...

  8. 信号之sigsuspend函数

    更改进程的信号屏蔽字可以阻塞所选择的信号,或解除对它们的阻塞.使用这种技术可以保护不希望由信号中断的代码临界区.如果希望对一个信号解除阻塞,然后pause等待以前被阻塞的信号发生,则又将如何呢?假定信 ...

  9. C#_delegate - Pair<T> 静态绑定

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. 梭子鱼:APT攻击是一盘更大的棋吗?

    随着企业对IT的依赖越来越强,APT攻击可能会成为一种恶意打击竞争对手的手段.目前,APT攻击目标主要有政治和经济目的两大类.而出于经济目的而进行的APT攻击可以获取竞争对手的商业信息,也可使用竞争对 ...