Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JEditorPane html文档内联(嵌入)图像来自文件_Java_Html_Swing_Base64_Jeditorpane - Fatal编程技术网

Java JEditorPane html文档内联(嵌入)图像来自文件

Java JEditorPane html文档内联(嵌入)图像来自文件,java,html,swing,base64,jeditorpane,Java,Html,Swing,Base64,Jeditorpane,我正在尝试将以下文件中的图像内联(嵌入)到JEditorPane中: <img src="data:image/gif;utf-8,data..."> 但我正在与代码作斗争 到目前为止,我有(假设是gif文件): 试试看 { File imageFile=新文件(“C:\\test\\testImage.gif”); 文件htmlFile=新文件(“C:\\test\\testOutput.html”); byte[]imageBytes=Files.toByteArray(

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

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

但我正在与代码作斗争

到目前为止,我有(假设是gif文件):

试试看
{
File imageFile=新文件(“C:\\test\\testImage.gif”);
文件htmlFile=新文件(“C:\\test\\testOutput.html”);
byte[]imageBytes=Files.toByteArray(imageFile);
字符串imageData=新字符串(imageBytes,“UTF-8”);
字符串html=“”;
writeStringToFile(htmlFile,htmlText);
}捕获(例外e){
e、 printStackTrace();
}
这会创建一个文件,但图像无效。我确信我没有以正确的方式转换图像…

JEditorPane
(以及通常的Java HTML呈现)不支持Base64编码图像

当然不是不能


问题是,您需要创建(或调整)一个可以定义新元素的模型。在图中可以看到一个例子。你需要寻找
HTML.tag.IMG
-这是一个标准图像,调用
super
功能,否则使用(或类似)将其转换为图像,然后嵌入它。

顺便说一句,我并不真正关心编码,它可以是base64。我能够让它工作。解释如下:不幸的是,我没有找到办法,所以我不得不相信你的答案:(
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();
}