本文介绍了带有嵌入式图像的JEditorPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图在Java JEditorPane中显示内联图像。下面的代码使用HTML内容,可以在Firefox中正确显示图像,但不能在JEditorPane中显示。任何想法为什么?谢谢。 import javax.swing。*; import java.awt。*; public class InlineImage { public InlineImage(){ JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JEditorPane edit = new JEditorPane(); frame.getContentPane()。add(edit); edit.setContentType(text / html); 字符串的html = LT; HTML><主体>局部图像与LT峰; br>< IMG SRC = \ 数据:图像/ PNG;字符集= UTF-8;的base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6 + AAACeklEQVR42u1bHZBCURgNgiBYCINgIVhYCIKFhSBYCIIgCIKFxSBoZpsJgjAIgmAhCIIgCIKFIAiChSAIF4IgCL7d82abnWl69Xq9 + 7r1Dhyp93PfOff7ufd + n8 / nEyF0AkmgIAQFoSDEjQgSCn1LPD6SbPZDSqWKNBqv0m5nZDh8lsnkUebziIH1OiC / d + wF的/ tteN50 + GPfiGbVaQcrld8nnm8Y78C4K8odAYC3R6Jfkci2pVosGaYtFWDYbvynRKgDx8G4Ij7FgTBjbzQuC2ZhOd4wZCgIOzfBLYysSxooxh8OL2xAEH4KPGo3irs98pwF3CZcXi42vS5CtCPiAaxfBDLPZvRQKNUWW49CDEomBdDrpmxXBDN1uSlKprvj9m8sLgkHAx47HMU + JYObSkBmenxDYvDGTaRum63UhdoFUG9maa4I​​gW4KZkvzD6PVebMaYEy6GSS6XdyTcIlaroA1rsRgr6vU3zwVsp4BFZzC4ckYQBCmYH4k9D4NBwmLAP2IZFMNZUY6nxwf + rFRKJNJhYLVvSxAs9Bgz1ADcniQIzIprDLVbL + aua8 + PyWSfxCkGOLYsSKuVI2mKAY4tC4LlP0lTv8ViWRAS5g4oyLUKQpelmctiUNcsqDPt1Szt5cJQs4Uht0402zrh5qKGm4tb19XvJ0mkq2ciPKC6ngOq3SNcEms / xXXsCJdFDhoWOeyWAdGFWSsDikTm7hXKwVq4VjEvlLNfWnpmKSkqGFlK + l9Kaj1WuFBs7cWKRrgmbYqtvdyOUCxW9W5HOCQOXBobdtjSxpY2J5o + L0W + 550 + 7bZFN5 t5JW3RT0 + fbIsmKAgFISgIBSHU4QdCoO0W7Xd4AwAAAABJRU5ErkJggg == \ >< /体>< / HTML> 中; edit.setText(html); frame.setSize(500,300); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String [] args){new InlineImage();} } 解决方案您需要为data:添加协议处理程序,以便为其打开URL / URLConnection。或者,您可以为类路径资源创建一些协议处理程序resource:。 您需要一个包 data 与类处理程序(固定名称约定!)。这将是data:的工厂类返回一个URLConnection。我们将为此创建DataConnection。 安装协议处理程序可以通过System.setProperty完成。在这里我提供了 Handler.install(); 以通用的方式来完成这项工作。 package test1.data; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; $ b $ public class Handler扩展了URLStreamHandler { @Override protected URLConnection openConnection(URL u)抛出IOException { return new DataConnection(u); public static void install(){ String pkgName = Handler.class.getPackage()。getName(); String pkg = pkgName.substring(0,pkgName.lastIndexOf('。')); String protocolHandlers = System.getProperty(java.protocol.handler.pkgs,); if(!protocolHandlers.contains(pkg)){ if(!protocolHandlers.isEmpty()){ protocolHandlers + =|; } protocolHandlers + = pkg; System.setProperty(java.protocol.handler.pkgs,protocolHandlers); $ b $ p $ b URLConnection给出一个InputStream字节: package test1.data; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import javax.xml.bind.DatatypeConverter; public class DataConnection extends URLConnection { public DataConnection(URL u){ super(u); } @Override public void connect()throws IOException { connected = true; $ b @Override public InputStream getInputStream()throws IOException { String data = url.toString(); data = data.replaceFirst(^。*; base64,,); System.out.println(Data:+ data); byte [] bytes = DatatypeConverter.parseBase64Binary(data); 返回新的ByteArrayInputStream(bytes); } } 这里的聪明之处在于使用标准Java SE中的 DatatypeConverter 的Base64解码。 现在可以使用。 I'm attempting to display an inline image in a Java JEditorPane. The code below uses HTML content that properly displays the image in Firefox, but not in the JEditorPane. Any ideas why? Thanks.import javax.swing.*;import java.awt.*;public class InlineImage { public InlineImage() { JFrame frame=new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JEditorPane edit=new JEditorPane(); frame.getContentPane().add(edit); edit.setContentType("text/html"); String html = "<html><body>Local image<br><img src=\"data:image/png;charset=utf-8;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAACeklEQVR42u1bHZBCURgNgiBYCINgIVhYCIKFhSBYCIIgCIKFxSBoZpsJgjAIgmAhCIIgCIKFIAiChSAIF4IgCL7d82abnWl69Xq9+7r1Dhyp93PfOff7ufd+n8/nEyF0AkmgIAQFoSDEjQgSCn1LPD6SbPZDSqWKNBqv0m5nZDh8lsnkUebziIH1OiC/d+wF/tteN50+GPfiGbVaQcrld8nnm8Y78C4K8odAYC3R6Jfkci2pVosGaYtFWDYbvynRKgDx8G4Ij7FgTBjbzQuC2ZhOd4wZCgIOzfBLYysSxooxh8OL2xAEH4KPGo3irs98pwF3CZcXi42vS5CtCPiAaxfBDLPZvRQKNUWW49CDEomBdDrpmxXBDN1uSlKprvj9m8sLgkHAx47HMU+JYObSkBmenxDYvDGTaRum63UhdoFUG9maa4IgW4KZkvzD6PVebMaYEy6GSS6XdyTcIlaroA1rsRgr6vU3zwVsp4BFZzC4ckYQBCmYH4k9D4NBwmLAP2IZFMNZUY6nxwf+rFRKJNJhYLVvSxAs9Bgz1ADcniQIzIprDLVbL+aua8+PyWSfxCkGOLYsSKuVI2mKAY4tC4LlP0lTv8ViWRAS5g4oyLUKQpelmctiUNcsqDPt1Szt5cJQs4Uht0402zrh5qKGm4tb19XvJ0mkq2ciPKC6ngOq3SNcEms/xXXsCJdFDhoWOeyWAdGFWSsDikTm7hXKwVq4VjEvlLNfWnpmKSkqGFlK+l9Kaj1WuFBs7cWKRrgmbYqtvdyOUCxW9W5HOCQOXBobdtjSxpY2J5o+L0W+55o+7bZFN5t5JW3RT0+fbIsmKAgFISgIBSHU4QdCoO0W7Xd4AwAAAABJRU5ErkJggg==\"></body></html>"; edit.setText(html); frame.setSize(500,300); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) {new InlineImage();}} 解决方案 You need to add a protocol handler for "data:" so an URL/URLConnection can be opened for it. Alternatively you could create some protocol handler "resource:" for class path resources.You need a package data with a class Handler (fixed name convention!). This will be the factory class for "data:" return an URLConnection. We will create DataConnection for that.Installing a protocol handler can be done via System.setProperty. Here I provided Handler.install(); to do that in a generic way.package test1.data;import java.io.IOException;import java.net.URL;import java.net.URLConnection;import java.net.URLStreamHandler;public class Handler extends URLStreamHandler { @Override protected URLConnection openConnection(URL u) throws IOException { return new DataConnection(u); } public static void install() { String pkgName = Handler.class.getPackage().getName(); String pkg = pkgName.substring(0, pkgName.lastIndexOf('.')); String protocolHandlers = System.getProperty("java.protocol.handler.pkgs", ""); if (!protocolHandlers.contains(pkg)) { if (!protocolHandlers.isEmpty()) { protocolHandlers += "|"; } protocolHandlers += pkg; System.setProperty("java.protocol.handler.pkgs", protocolHandlers); } }}The URLConnection gives an InputStream to the bytes:package test1.data;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;import javax.xml.bind.DatatypeConverter;public class DataConnection extends URLConnection { public DataConnection(URL u) { super(u); } @Override public void connect() throws IOException { connected = true; } @Override public InputStream getInputStream() throws IOException { String data = url.toString(); data = data.replaceFirst("^.*;base64,", ""); System.out.println("Data: " + data); byte[] bytes = DatatypeConverter.parseBase64Binary(data); return new ByteArrayInputStream(bytes); }}The clever thing here is to use Base64 decoding of DatatypeConverter in standard Java SE.P.S.Nowadays one would use Base64.getEncoder().encode(...). 这篇关于带有嵌入式图像的JEditorPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-27 14:46