java - JEdi​​torPane html 文档内联(嵌入)文件中的图像

标签 java html swing base64 jeditorpane

我正在尝试将图像从以下文件内联(嵌入)到 JEditorPane 中:

<img src="data:image/gif;utf-8,data..."> 

但是我在编写代码时遇到了困难。

到目前为止我已经(假设是 gif 文件):

try
{
    File imageFile = new File("C:\\test\\testImage.gif"); 
    File htmlFile = new new File("C:\\test\\testOutput.html");

    byte[] imageBytes = Files.toByteArray(imageFile);
    String imageData = new String(imageBytes, "UTF-8");

    String html = "<html><body><img src=\"data:image/gif;utf-8," + imageData + "\"></body></html>";

    FileUtils.writeStringToFile(htmlFile, htmlText);

} catch (Exception e) {
    e.printStackTrace();
}

这确实创建了一个文件,但图像无效。我确信我没有以正确的方式转换图像......

最佳答案

JEditorPane(以及一般的 Java HTML 渲染)不支持 Base64 编码图像。

当然“不”!=“不能”。

问题是,您需要创建(或调整)EditorKit可以定义新元素。一个例如见AppletEditorKit 。您需要查找 HTML.tag.IMG - 它是标准图像,调用 super 功能,否则使用 this source (或类似)将其转换为图像,然后嵌入它。

关于java - JEdi​​torPane html 文档内联(嵌入)文件中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17636804/

相关文章:

java - 如何在 Java 中使用 facebook connect 和 scribe 库注销用户?

java - 从jar中读取资源文件

jquery - 将 div 作为字符串返回 - 但仅显示可见部分

html - 顶部和底部类型为 "arrow"的 Div 容器

java - MouseListener 中的 getX 和 getY 问题

java - 在我的屏幕上放置重新启动按钮

java - 如何将所有按钮放在同一行?

java - 从 Spring BindingResult 到字段 JSONPath/JSON Pointer,与 Jackson

python - 当参数为 ='' 时,使用 Beautiful Soup 访问属性参数

java - 如何在paintComponent方法中使用命令参数?