简体   繁体   English

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

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

I'm getting an html response from a server that I would like to display in a JEditorPane. 我从服务器显示了html响应,我想在JEditorPane中显示它。 But the response sets the charset to windows-1252 which seems to cause none of the html to render. 但是响应将字符集设置为Windows-1252,这似乎不会导致任何html呈现。 (when I comment it out it renders just fine). (当我将其注释掉时,它就很好了)。

So, one solution would be to parse that out before I try to display it, but I was wondering if there's a known bug or anything else I can do to avoid editing the response. 因此,一种解决方案是在尝试显示它之前先将其解析出来,但是我想知道是否存在已知的错误或可以做其他避免编辑响应的措施。 Thanks! 谢谢!

In case you want to try it (you'll see it displays if you comment out the 3rd line): 如果您想尝试一下(如果您注释掉第三行,您会看到它显示):

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);
}

There is a bug: 4695909 : JEditorPane fails to display HTML BODY when META tag included in HEAD section 有一个错误: 4695909:当HEAD部分中包含META标签时,JEditorPane无法显示HTML BODY

But you can use a directive to ignore that META tag: 但是您可以使用指令忽略该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