Awt剪贴板和MIME类型
InsideClipboard显示内容的MIME类型为
application/sparkeditor
您应该能够通过使用构造函数创建MIME类型DataFlavor,
DataFlavor(String mimeType, StringhumanReadableFormat)在这种情况下,类表示形式将是一种
InputStream您可以从中提取字节的经典方式…
但是,此剪贴板实现对mime类型定义非常严格,并且您不能使用ID格式的空格,这太糟糕了,因为您的编辑器似乎在其中放置了一个空格:(
可能的解决方案,如果您有权访问JavaFX
JavaFX的剪贴板管理更加宽松,并且可以容纳剪贴板中的各种“格式名称”(InsideClipboard称呼它们),而不仅仅是
type/subtypeawt中的无空格哑剧格式。
例如,使用LibreOffice Draw 4.2并复制一个Rectangle形状,awt仅看到一种
application/x-java-rawimage格式,而JavaFX看到与InsideClipboard相同的所有格式:
[application / x-java-rawimage],[PNG],[Star Object
Descriptor(XML)],[cf3],[Windows位图],[GDImetaFile],[cf17],[Star Embed
Source(XML)],[图纸格式]
然后,您可以从JavaFX剪贴板中获取原始数据
java.nio.ByteBuffer
//with awtDataFlavor[] availableDataFlavors = Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors();System.out.println("Awt detected flavors : "+availableDataFlavors.length);for (DataFlavor f : availableDataFlavors) { System.out.println(f);}//with JavaFX (called from JavaFX thread, eg start method in a javaFX ApplicationSet<DataFormat> contentTypes = Clipboard.getSystemClipboard().getContentTypes();System.out.println("JavaFX detected flavors : " + contentTypes.size());for (DataFormat s : contentTypes) { System.out.println(s);}//let's attempt to extract bytes from the clipboard containing data from the game editor// (note : some types will be automatically mapped to Java classes, and unknown types to a ByteBuffer)// another reproducable example is type "Drawing Format" with a Rectangle shape copied from LibreOffice Draw 4.2DataFormat df = DataFormat.lookupMimeType("application/spark editor");if (df != null) { Object content = Clipboard.getSystemClipboard().getContent(df); if (content instanceof ByteBuffer) { ByteBuffer buffer = (ByteBuffer) content; System.err.println(new String(buffer.array(), "UTF-8")); } else System.err.println(content);}


