概述Java剪贴板:在Linux上粘贴Firefox的HTML
从firefox粘贴HTML到linux上的Java6应用程序(仅!)时,出现了一个奇怪的问题。 这是一个最小的例子:
import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.io.Reader; import java.nio.ByteBuffer; class ClipboardPrinter { public static voID main( String args[] ) throws Exception { Clipboard systemClipboard = Toolkit.getDefaultToolkit() .getSystemClipboard(); Transferable transferData = systemClipboard.getContents(null); if (transferData == null) { System.out.println(\”no content\”); return; } // final DataFlavor HTMLFlavorString = new DataFlavor(\”text/HTML;class=java.lang.String\”); // String HTML = (String)transferData.getTransferData(HTMLFlavorString); // System.out.println(\”HTML = \’\” + HTML + \”\’\”); final DataFlavor HTMLFlavor = new DataFlavor(\”text/HTML;class=java.nio.ByteBuffer;charset=US-ASCII\”); if (!transferData.isDataFlavorSupported(HTMLFlavor)) { System.out.println(\”no text/HTML reader content\”); return; } ByteBuffer bb = (ByteBuffer)transferData.getTransferData(HTMLFlavor); byte[] bytes = bb.array(); for (byte b: bytes) { System.out.format(\”%02x\”,b); } System.out.println(); final int cutoff = 2; byte[] bytes2 = new byte[bytes.length – cutoff]; for (int i = cutoff; i < bytes.length; i++) bytes2[i-cutoff] = bytes[i]; final String HTMLContent = new String(bytes2,\”UTF-16LE\”); System.out.println(\”HTMLContent = \’\” + HTMLContent + \”\’\”); } }
首先,我尝试使用new DataFlavor(\”text/HTML;class=java.lang.String\”) (代码注释在上面的代码片段中),但是这会导致一个不可用的string,其中包含2个字符,其值为65533这无助于切断这两个字符)。
接下来,我使用了charset=US-ASCII (我charset=UTF-16LE使用ASCII)的ByteBuffer数据types: charset=UTF-16LE (或UTF-16或UTF-16BE)完全不起作用。 使用上面的charset=US-ASCII解决scheme(以及new String(bytes2,\”UTF-16LE\”) ),7位字符工作(但是,例如,元音变音不起作用,取而代之的是打印\’?\’)。
我切断了两个字节,因为在开始时似乎有两个boms(不知道,可能是其他的东西)?
将以XML格式存储的SVG图像复制到windows剪贴板,如image / svg + xml MIMEtypes
连接到剪贴板?
windows 10 linux子系统 – Python – string到电脑剪贴板
为什么这个autohotkey脚本无法一致地保存剪贴板内容?
从系统剪贴板到gVIM的粘贴function
使用charset=UTF-8和cutoff = 6(两个三字节“replace字符”0xEFBFBD在开头,umlaut编码为两个错误字符),我得到了一个类似的结果。 在这两种情况下,我用new String(bytes2,\”UTF-16LE\”) 。
你有什么build议如何:
在此解决scheme中支持非ASCII字符(或find更好的解决scheme)?
确定它是UTF-16LE还是UTF-16BE?
谢谢! 任何提示都表示赞赏!
BTW:以下是我的(linux)系统(来自transferable.getTransferDataFlavors() )支持的数据风格:
[java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.Reader] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.lang.String] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.CharBuffer] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[C] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.inputStream;charset=UTF-16] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.ByteBuffer;charset=UTF-16] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[B;charset=UTF-16] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.inputStream;charset=UTF-8] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.ByteBuffer;charset=UTF-8] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[B;charset=UTF-8] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.inputStream;charset=UTF-16BE] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.ByteBuffer;charset=UTF-16BE] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[B;charset=UTF-16BE] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.inputStream;charset=UTF-16LE] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.ByteBuffer;charset=UTF-16LE] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[B;charset=UTF-16LE] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.inputStream;charset=ISO-8859-1] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.ByteBuffer;charset=ISO-8859-1] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[B;charset=ISO-8859-1] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.io.inputStream;charset=US-ASCII] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=java.nio.ByteBuffer;charset=US-ASCII] java.awt.datatransfer.DataFlavor[mimetype=text/HTML;representationclass=[B;charset=US-ASCII] java.awt.datatransfer.DataFlavor[mimetype=application/x-java-serialized-object;representationclass=java.lang.String] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.Reader] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.lang.String] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.CharBuffer] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[C] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.inputStream;charset=unicode] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.inputStream;charset=UTF-8] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-8] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-8] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.inputStream;charset=UTF-16BE] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16BE] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16BE] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.inputStream;charset=UTF-16LE] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16LE] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16LE] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.inputStream;charset=ISO-8859-1] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=ISO-8859-1] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=ISO-8859-1] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.inputStream;charset=US-ASCII] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=US-ASCII] java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=US-ASCII] java.awt.datatransfer.DataFlavor[mimetype=text/x-moz-url-priv;representationclass=java.io.inputStream] java.awt.datatransfer.DataFlavor[mimetype=text/_moz_HTMLinfo;representationclass=java.io.inputStream] java.awt.datatransfer.DataFlavor[mimetype=text/_moz_HTMLcontext;representationclass=java.io.inputStream] java.awt.datatransfer.DataFlavor[mimetype=text/x-moz-url-priv;representationclass=java.nio.ByteBuffer] java.awt.datatransfer.DataFlavor[mimetype=text/_moz_HTMLinfo;representationclass=java.nio.ByteBuffer] java.awt.datatransfer.DataFlavor[mimetype=text/_moz_HTMLcontext;representationclass=java.nio.ByteBuffer] java.awt.datatransfer.DataFlavor[mimetype=text/x-moz-url-priv;representationclass= java.awt.datatransfer.DataFlavor[mimetype=text/_moz_HTMLinfo;representationclass=[B] java.awt.datatransfer.DataFlavor[mimetype=text/_moz_HTMLcontext;representationclass=[B]]
在windows中将string转义转义为subprocess调用
在python3中将图像复制到剪贴板
使用VBScript清除剪贴板
如何判断windows剪贴板上的文本是C ++中的ISO 8859还是UTF-8?
访问windows剪贴板
我相信这个问题是相关的,因为他read from clipboard as US-ASCII ,然后转换为unicode,并希望完整地保留德文变音符号。 由于US-ASCII是一个7位字符集,德文变音符号不包含在US-ASCII中读取剪贴板后已经丢失。
public class CharsetDemo { public static voID main(String[] args) throws Exception { byte[] bytes; // convert the German umlaut to bytes in US-ASCII charset bytes = \”ö\”.getBytes(\”US-ASCII\”); System.out.println(\”US-ASCII\”); System.out.println(\”bytes : \” + asHexString(bytes)); System.out.println(\”string: \” + new String(bytes,\”US-ASCII\”)); System.out.println(); // create a unicode string from the US-ASCII bytes String utf8String = new String(bytes,\”UTF-8\”); bytes = utf8String.getBytes(\”UTF-8\”); System.out.println(\”UTF-8\”); System.out.println(\”bytes : \” + asHexString(bytes)); System.out.println(\”string: \” + utf8String); System.out.println(); // convert the German umlaut to bytes in ISO-8859-1 charset bytes = \”ö\”.getBytes(\”ISO-8859-1\”); System.out.println(\”ISO 8859-1\”); System.out.println(\”bytes : \” + asHexString(bytes)); System.out.println(\”string: \” + new String(bytes,\”ISO-8859-1\”)); System.out.println(); // create a unicode string from the ISO-8859-1 bytes utf8String = new String(bytes,\”UTF-8\”); bytes = utf8String.getBytes(\”UTF-8\”); System.out.println(\”UTF-8\”); System.out.println(\”bytes : \” + asHexString(bytes)); System.out.println(\”string: \” + utf8String); System.out.println(); // bytes of the \”REPLACEMET CHaraCTER\” System.out.println(\”replacement character bytes: \” + asHexString(\”uFFFD\”.getBytes(\”UTF-8\”))); } static String asHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format(\”%X \”,b)); } return sb.toString(); } }
[b]产量
US-ASCII bytes : 3F string: ? <— the question mark represents here the \”REPLACEMENT CHaraCTER\” UTF-8 bytes : 3F string: ? ISO 8859-1 bytes : F6 string: ö UTF-8 bytes : EF BF BD <– the \”REPLACEMENT CHaraCTER\”,as \”F6\” is not a valID UTF-8 codepoint string: replacement character bytes: EF BF BD
Java 6不再支持。 所以,问题已经过时了。
总结
以上是内存溢出为你收集整理的Java剪贴板:在Linux上粘贴Firefox的HTML全部内容,希望文章能够帮你解决Java剪贴板:在Linux上粘贴Firefox的HTML所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
请登录后查看评论内容