Java系统剪贴板可以复制文件吗? [英] Can Java system clipboard copy a file?

查看:125
本文介绍了Java系统剪贴板可以复制文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java系统剪贴板传输文本和图像,但我想知道它是否可以复制和粘贴文件?如果是这样,我在哪里可以找到一些示例代码?

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 ?

我发现了一个类似的问题:如何使用Java复制文件并将其粘贴到剪贴板?

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 ?

推荐答案

基本上,是的。您需要记住,拖放API和剪贴板API都使用相同的 Transferable 概念,它将数据包装到 DataFlavor中 s,因此您可以根据目标系统想要使用的风格来不同地传输数据

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

通常,在传输文件时,Java使用a java.util.List DataFlavor.javaFileListFlavor 。不用说,这里没有很好的包装课程,所以你需要自己提供,例如......

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;
        }
    }

}

在我的测试,我能够在列表中放置文件,换行 Transferable ,将其传递给剪贴板,并能够通过系统粘贴文件(Windows资源管理器)

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)

这篇关于Java系统剪贴板可以复制文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆