Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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 显示base64编码图像_Java_Html_Jtextpane - Fatal编程技术网

Java 显示base64编码图像

Java 显示base64编码图像,java,html,jtextpane,Java,Html,Jtextpane,我在文本/html JTextPane中集成图像时遇到问题。 JTextPane用以下文本初始化: 我插入以下文字: kit.insertHTML(doc,doc.getLength(),“”+string+“”,0,0,HTML.Tag.B); 以这种方式插入的所有文本都会正确显示,但当我尝试插入带有以下内容的base64编码图像时: kit.insertHTML(doc,doc.getLength(),“”,0,0,HTML.Tag.IMG); 我只得到了一个占位符图像。当尝试使用正

我在文本/html JTextPane中集成图像时遇到问题。 JTextPane用以下文本初始化:


我插入以下文字:

kit.insertHTML(doc,doc.getLength(),“”+string+“
”,0,0,HTML.Tag.B);
以这种方式插入的所有文本都会正确显示,但当我尝试插入带有以下内容的base64编码图像时:

kit.insertHTML(doc,doc.getLength(),“”,0,0,HTML.Tag.IMG);
我只得到了一个占位符图像。当尝试使用正常的源路径时,它成功了。然而,在线获取base64代码并使用它,我也得到了一个占位符图像,而w3school.com的HTML tryit编辑器上也使用了完全相同的代码。

当a看到
标记时,它会检查图像是否存在于缓存中,如果不存在,它会尝试从url读取图像。
JTextPane
使用的html库在
标记中不支持base64编码的图像数据,因此我们需要以不同的方式进行

事实证明,我们可以手动将图像添加到图像缓存中。这可以用来选择一些无效的url并为其分配一个图像


让我们将图像添加到缓存中,并在
JTextPane
中显示它

首先,您要将图像转换为图像。这可以使用类来完成

注意,这里我们需要原始图像字节,而不是base64编码。如果正在从文件读取图像,则可以将a传递给
read
函数,而不是输入流


现在我们将图像作为
缓冲区图像
,我们可以编写一个函数将其添加到缓存中

byte[] imgBytes = decodeBase64(base64Code);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));
请注意,我在
Dictionary
Hashtable
上抑制了一些关于类型参数的警告。通常应该避免这种情况,但在这种情况下,我们处理Swing胡说八道的方式是可以抑制警告

这个方法实质上是选取一些无效的url并将图像存储在该url上

注意
name
参数。这将是url的一部分,如果您尝试使用与前一个图像相同的名称将图像存储到缓存中,这将替换前一个图像。避免在此名称中使用疯狂字符,因为
新Url(Url)
如果不是有效的Url,可能会抛出错误


我们现在可以使用它


您还可以通过调用
setContentType
或用新对象替换
JTextPane
来清除缓存。这是因为缓存存储在
JTextPane

Base64字符串中可能包含=之类的字符,这些字符将破坏HTML,除非在构建HTML时对它们进行编码。例如,=应该是%3谢谢,我试试看。我在找一张这样的人物图表,你已经有了吗?编辑:已经找到了这个(),但是这里没有提到=。只需通过java.net.URLEncoder.encode(base64Code,“UTF-8”)传递base64字符串即可。谢谢,我明天会尝试一下。这绝对是解决方案,它工作得完美无缺。谢谢你的发帖,我不知道它甚至不能处理base64。
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String saveImageToCache(JTextPane pane, BufferedImage img, String name) throws MalformedURLException {
    Dictionary cache = (Dictionary) pane.getDocument().getProperty("imageCache");
    if (cache == null) {
        // No cache exists, so create a new one.
        cache = new Hashtable();
        pane.getDocument().putProperty("imageCache", cache);
    }
    String url = "http:\\buffered/" + name;
    cache.put(new URL(url), img);
    return url;
}
BufferedImage img = ...;

JTextPane pane = new JTextPane();
pane.setContentType("text/html");

String url = saveImageToCache(pane, img, "image1");

pane.setText("<html><body><img src=\"" + url + "\"></body></html>");

JFrame frame = new JFrame("image test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(pane);
frame.setSize(img.getWidth(), img.getHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
@SuppressWarnings({ "rawtypes" })
public static void removeImageFromCache(JTextPane pane, String name) throws MalformedURLException {
    Dictionary cache = (Dictionary) pane.getDocument().getProperty("imageCache");
    if (cache == null) {
        // There is no cache, so the image is not in the cache.
        return;
    }
    String url = "http:\\buffered/" + name;
    cache.remove(new URL(url));
}