0

I'm getting an html response from a server that I would like to display in a JEditorPane. But the response sets the charset to windows-1252 which seems to cause none of the html to render. (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);
}
4

1 回答 1

1

有一个错误: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);
于 2013-06-17T16:41:22.057 回答