繁体   English   中英

如何在JEditorPane中使用charset = windows-1252显示html

[英]How to display html with charset=windows-1252 in JEditorPane

我从服务器显示了html响应,我想在JEditorPane中显示它。 但是响应将字符集设置为Windows-1252,这似乎不会导致任何html呈现。 (当我将其注释掉时,它就很好了)。

因此,一种解决方案是在尝试显示它之前先将其解析出来,但是我想知道是否存在已知的错误或可以做其他避免编辑响应的措施。 谢谢!

如果您想尝试一下(如果您注释掉第三行,您会看到它显示):

public static void main(String args [])
{
    String html = "<!DOCTYPE HTML PUBLIC \"-////W3C////DTD HTML 4.0 Transitional//EN\">" +
        "<HTML>" +
        "<HEAD>" +
        "<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">" +
        "</HEAD>" +
        "<BODY>" +
        "<P>Hello World</P>" +
        "</BODY>" +
        "</HTML>";
    JEditorPane editor = new JEditorPane("text/html", html);
    editor.setEditable(false);
    JScrollPane pane = new JScrollPane(editor);
    JFrame f = new JFrame("charset=windows-1252");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(pane);
    f.setSize(800, 600);
    f.setVisible(true);
}

有一个错误: 4695909:当HEAD部分中包含META标签时,JEditorPane无法显示HTML BODY

但是您可以使用指令忽略该META标签:

JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.getDocument().putProperty("IgnoreCharsetDirective", true);
editor.setText(html);
editor.setEditable(false);

JScrollPane pane = new JScrollPane(editor);
JFrame f = new JFrame("charset=windows-1252");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(pane);
f.setSize(800, 600);
f.setVisible(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM