(Java)通过剪贴板将对象传输到应用程序

时间:2016-01-24 11:31:08

标签: java serialization clipboard

如何将Java对象(带有几个字段)复制到我的剪贴板中,以及存储这些字段的最佳方法是什么? 我有一个带ΗashΜap的简单对象用于存储。

另外,我为这个对象创建了一个选择,它实现了TransferableClipboardOwner,以及DataFlavor

我要做的是复制此对象并将其粘贴到另一个应用程序中。请注意,我无法访问此应用程序的源代码,因此我必须以特定的方式传输此对象。我们假设此应用程序需要以下字段:

  • 信息(字符串)
  • 文字(UTF-8格式的字符串)

但是,将此对象复制并粘贴到其他应用程序似乎无法正常工作。

下面你可以看到我的课程:

为MyObject

import java.util.HashMap;

public class MyObject {

    private HashMap<Object, Object> data;

    public MyObject() 
    {
        data = new HashMap<Object, Object>();
    }

    public void setData(Object field, Object value) 
    {
        data.put(field, value);
    }

}

MyObjectFlavor

import java.awt.datatransfer.DataFlavor;

public class MyObjectFlavor extends DataFlavor {

    public MyObjectFlavor() 
    {
        super(MyObject.class, null);
    }  

}    

MyObjectSelection

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class MyObjectSelection implements Transferable, ClipboardOwner {

    final static DataFlavor myObjectFlavor = new MyObjectFlavor();
    final static DataFlavor[] supportedDataFlavors = { myObjectFlavor };

    private MyObject myObject;

    public MyObjectSelection(MyObject myObject) 
    {
        this.myObject = myObject;
    }

    public Object getTransferData(DataFlavor dataFlavor)
            throws UnsupportedFlavorException, IOException 
    {
        if (!isDataFlavorSupported(dataFlavor)) {
            throw new UnsupportedFlavorException(dataFlavor);
        }

        // Passing data in a ByteArrayOutputStream.

        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(myObject);
        return byteStream.toByteArray();
    }

    public DataFlavor[] getTransferDataFlavors() 
    {
        return supportedDataFlavors;
    }

    public boolean isDataFlavorSupported(DataFlavor dataFlavor) 
    {

        for (int i = 0; i < supportedDataFlavors.length; i++) {
            if (supportedDataFlavors[i].equals(dataFlavor)) {
                return true;
            }
        }
        return false;

    }

    public void lostOwnership(Clipboard arg0, Transferable arg1) 
    {
        // Lost ownership.
    }

}

最后,我的 Main 类是:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class Main {
    public static void main(String args[]) throws IOException {

        MyObject myObject = new MyObject();
        MyObjectSelection selection = new MyObjectSelection(myObject);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard ();

        String myUtfString = "This is a String in UTF-8";

        myObject.setData("Info", "This is a Java Object.");
        myObject.setData("Text", new ByteArrayInputStream(myUtfString.getBytes("UTF-8")));

        clipboard.setContents(selection, selection);

    }
}

我很抱歉这篇长篇文章,但我认为提供所有课程都会有所帮助。我希望这个问题不具体。 提前谢谢!

0 个答案:

没有答案