粘贴板操作是很有用的,比如你需要粘贴一段文字,不想要他的格式只想要他的文字或者格式和文字都想要,你复制一个图片信息粘贴导致本地的时候想保存为一个文件,系统是怎么实现的呢。
真相
系统的粘贴功能其实有一个规范的规范编号为:rfc2045
英文名:Multipurpose Internet Mail Extensions (MIME) Part One:Format of Internet Message Bodies
中文翻译:多用途多格式邮件扩展-信体格式说明书
链接:http://www.ietf.org/rfc/rfc2045.txt
这个规范主要描述了网络数据协议格式.格式为:
entity-headers := [ content CRLF ]
[ encoding CRLF ]
[ id CRLF ]
[ description CRLF ]
*( MIME-extension-field CRLF )
MIME-message-headers := entity-headers
fields
version CRLF
; The ordering of the header
; fields implied by this BNF
; definition should be ignored.
MIME-part-headers := entity-headers
[ fields ]
; Any field not beginning with
; "content-" can have no defined
; meaning and may be ignored.
; The ordering of the header
; fields implied by this BNF
; definition should be ignored.
#其中 Content-type:
#image/x-java-image
#Content-type: text/plain; charset=us-ascii
#...等格式类型
好了以上是一般操作系统粘贴使用的粘贴板协议概述。
详细可以参照rfc2045
Java中的粘贴板
java 中使用awt实现rfc2045 协议,
java.awt.datatransfer.Transferable
java.awt.datatransfer.DataFlavor
java.awt.datatransfer.Clipboard
这三个类实现RFC2045协议
具体Demo代码
package com.ghw.test;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStreamReader;
import java.util.List;
/**
* Created by cocky on 2017/3/1.
*/
public class TestPaste {
public String getClipboardText(Clipboard clip) throws Exception {
Transferable clipT = clip.getContents(null);
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
return (String) clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
public BufferedImage getClipboardImage(Clipboard clip) throws Exception {
Transferable clipT = clip.getContents(null);
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.imageFlavor))
return (BufferedImage) clipT.getTransferData(DataFlavor.imageFlavor);
else {
System.out.println("Not support");
}
}
return null;
}
public InputStreamReader getClipboardplainInputStream(Clipboard clip) throws Exception {
Transferable clipT = clip.getContents(null);
if (clipT != null) {
//自动识别最好的Flavor
if (clipT.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor()))
return (InputStreamReader)clipT.getTransferData(DataFlavor.selectBestTextFlavor(clipT.getTransferDataFlavors()));
else {
System.out.println("Not support");
}
}
return null;
}
public java.util.List<File> getClipboardFile(Clipboard clip) throws Exception {
Transferable clipT = clip.getContents(null);
if (clipT != null) {
//自动识别最好的Flavor
if (clipT.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
return (java.util.List<File>)clipT.getTransferData(DataFlavor.javaFileListFlavor);
else {
System.out.println("Not support");
}
}
return null;
}
public static void main(String[] args) throws Exception {
TestPaste testpaste = new TestPaste();
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//直接就是文本,也可以支持把html转为text
System.out.println("===:"+testpaste.getClipboardText(systemClipboard));
//图片
System.out.println("===:"+testpaste.getClipboardImage(systemClipboard));
//二进制流
//InputStreamReader paste = testparse.getClipboardplainInputStream(systemClipboard);
//System.out.println(IOUtils.toString(paste));
List<File> clipboardFile = testpaste.getClipboardFile(systemClipboard);
System.out.println(clipboardFile);
}
}