gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:18:53 25 4
gpt4 key购买 nike

我正在尝试将图像从以下文件内联(嵌入)到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com