Can Java system clipboard copy a file?

2020-02-01 18:39发布

I've used Java system clipboard to transfer text and image, but I wonder if it can copy and paste files ? If so where can I find some sample code ?

I found a similar question at : How can I copy a file and paste it to the clipboard using Java?

But nowhere in it can I find the word "clipboard", and I don't know how to use it.

The methods I use to copy image look like this :

  public static void setClipboard(Image image)                                           // This method writes a image to the system clipboard : from exampledepot.com
  {
    ImageSelection imgSel=new ImageSelection(image);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel,null);
  }

  public Image getImageFromClipboard()                                                   // Get an image off the system clipboard 
  {
    Transferable transferable=Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (transferable!=null&&transferable.isDataFlavorSupported(DataFlavor.imageFlavor))
    {
      try { return (Image)transferable.getTransferData(DataFlavor.imageFlavor); }
      catch (UnsupportedFlavorException e) { e.printStackTrace(); }
      catch (IOException e) { e.printStackTrace(); }
    }
    else System.err.println("getImageFromClipboard: That wasn't an image!");
    return null;
  }

How to modify the above code to transfer files ?

1条回答
劫难
2楼-- · 2020-02-01 18:45

Essentially, yes. You need to remember that both the drag'n'drop API and the clipboard API use the same concept of a Transferable, which wraps the data into DataFlavors, so you can transfer the data differently based on the flavor the target system would like to use

Generally, when transferring files, Java uses a java.util.List and a DataFlavor.javaFileListFlavor. Unfourtantly, there's no nice "wrapper" class available for this, so you'll need to provide your own, for example...

import java.awt.Toolkit;
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.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        File file = new File("/path/to/your/file");
        List listOfFiles = new ArrayList();
        listOfFiles.add(file);

        FileTransferable ft = new FileTransferable(listOfFiles);

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ft, new ClipboardOwner() {
            @Override
            public void lostOwnership(Clipboard clipboard, Transferable contents) {
                System.out.println("Lost ownership");
            }
        });
    }

    public static class FileTransferable implements Transferable {

        private List listOfFiles;

        public FileTransferable(List listOfFiles) {
            this.listOfFiles = listOfFiles;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.javaFileListFlavor};
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.javaFileListFlavor.equals(flavor);
        }

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            return listOfFiles;
        }
    }

}

In my tests, I was able to place a File in the List, wrap into a Transferable, pass it to the Clipboard and was able to paste the file through the system (Windows Explorer)

查看更多
登录 后发表回答