本文目录导读:

这个异常 JEditorPaneHTMLEditorKitParserMethodUnsupportedEncodingException 表明在使用 JEditorPane 和 HTMLEditorKit 解析 HTML 时遇到了不支持的字符编码。
常见原因
-
HTML 中声明的编码不受支持
<meta charset="GB2312"> <!-- 可能不支持 --> <meta charset="Shift_JIS"> <!-- 可能不支持 -->
-
从流/文档读取时编码不匹配
-
使用 Java 版本较旧,编码支持有限
解决方案
设置正确的编码
import javax.swing.*;
import javax.swing.text.html.*;
import java.io.*;
public class HTMLViewer {
public static void main(String[] args) {
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html; charset=UTF-8");
// 方法1:使用setPage
try {
editorPane.setPage("http://example.com");
} catch (IOException e) {
e.printStackTrace();
}
// 方法2:自定义读取
try {
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
// 使用指定编码读取
Reader reader = new InputStreamReader(
new FileInputStream("file.html"),
"UTF-8" // 使用支持的编码
);
kit.read(reader, doc, 0);
editorPane.setDocument(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
自定义编码过滤器
import javax.swing.text.html.*;
import javax.swing.text.*;
import java.io.*;
public class CustomHTMLEditorKit extends HTMLEditorKit {
@Override
public Document createDefaultDocument() {
HTMLDocument doc = new HTMLDocument() {
// 覆盖解析器行为
};
return doc;
}
@Override
public void read(Reader in, Document doc, int pos)
throws IOException, BadLocationException {
// 预处理:修改编码声明
StringBuilder content = new StringBuilder();
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
// 替换不支持的编码声明
line = line.replaceAll(
"charset=[\"']?[\\w-]+[\"']?",
"charset=UTF-8"
);
content.append(line).append("\n");
}
StringReader reader = new StringReader(content.toString());
super.read(reader, doc, pos);
}
}
使用 SwingWorker 异步处理
import javax.swing.*;
import java.util.concurrent.*;
public class AsyncHTMLReader extends SwingWorker<Void, Void> {
private JEditorPane editorPane;
private String url;
public AsyncHTMLReader(JEditorPane pane, String url) {
this.editorPane = pane;
this.url = url;
}
@Override
protected Void doInBackground() throws Exception {
// 在主线程外处理编码问题
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
URLConnection connection = new URL(url).openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(
connection.getInputStream(),
"UTF-8" // 强制使用UTF-8
)
);
String line;
StringBuilder html = new StringBuilder();
while ((line = reader.readLine()) != null) {
html.append(line).append("\n");
}
reader.close();
// 替换不支持的编码
String cleanHtml = html.toString()
.replaceAll("charset=[\"']?[\\w-]+[\"']?", "charset=UTF-8");
SwingUtilities.invokeLater(() -> {
try {
kit.read(
new StringReader(cleanHtml),
doc,
0
);
editorPane.setDocument(doc);
} catch (Exception e) {
e.printStackTrace();
}
});
return null;
}
}
支持的编码列表
Java 通常支持的编码包括:
- UTF-8
- ISO-8859-1 (Latin-1)
- US-ASCII
- UTF-16
- GBK (某些Java版本)
- EUC-JP
调试技巧
// 捕获并查看具体错误
try {
editorPane.setPage("URL");
} catch (UnsupportedEncodingException e) {
System.err.println("不支持的编码: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
推荐方案
- 始终使用 UTF-8(最兼容)
- 预处理 HTML 内容,替换编码声明
- 如果必须支持多种编码,考虑使用第三方库如 Jsoup
<!-- Maven依赖 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.1</version>
</dependency>
使用 Jsoup 处理更复杂的编码问题会更简单可靠。