2013-06-17 2 views
0

JEditorPane에 표시하려는 서버에서 HTML 응답을받습니다. 그러나 응답은 charset을 windows-1252로 설정합니다. html이 렌더링되지 않는 것으로 보입니다. (내가 그것을 주석으로 달아도 괜찮아요.)JEditorPane에서 charset = 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); 
} 

답변

1

버그가 있습니다 : 경우

당신이 (당신이 3 줄을 주석 경우이 표시가 나타납니다)을 시도하려는 4695909 : JEditorPane fails to display HTML BODY when META tag included in HEAD section

그러나 당신은 지시어를 사용할 수 있습니다 그것을 무시하는 것 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); 
+0

그걸해야합니다! 고마워. – Amber