简体   繁体   English

Java-下载文件后从剪贴板粘贴文件

[英]Java - Paste a file from clipboard after it has been downloaded

I have some trouble with the Windows Explorer. 我在Windows资源管理器中遇到了一些麻烦。

My Application starts the download of a file and puts it into the system clipboard meanwhile. 我的应用程序开始下载文件,然后将其放入系统剪贴板。 If the User now pastes the file at some place of the Windows Explorer, the file isn't having its real size, but the size depending on the progress of the download. 如果用户现在将文件粘贴到Windows资源管理器的某个位置,则该文件没有其实际大小,但是大小取决于下载进度。

Does somebody have an idea of how I could advise the Explorer to wait until the download has finished and copy the file to the target directory afterwards? 有人知道我如何建议资源管理器等到下载完成后再将文件复制到目标目录吗?

I also tried to "tunnel" the file over the loopback Interface using the local SMB server but it didnt help unfortunately... 我还尝试使用本地SMB服务器在回送接口上“隧道化”文件,但不幸的是,它没有帮助...

Thanks in advance 提前致谢

Downloading the file and calling the paste to clipboard method: 下载文件并调用粘贴到剪贴板方法:

new Thread(new Runnable() {

            @Override
            public void run() {

                final String where = getText();

                int selectedRows[] = CustomJTableModel.table.getSelectedRows();

                List<File> fileList = new ArrayList<File>();

                for( int i=0; selectedRows.length>i; i++ ) {

                    // Build the relative file/folder path

                    String fileName = (String)table.getValueAt(table.getSelectedRow(), 0);

                    final String value = buildPath(where, fileName);

                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            staticNetworkDaemon.getFile(value, where);

                        }

                    }).start();

                    fileList.add(new File("\\\\127.0.0.1\\r$\\downloaded" + value.replace("/", "\\")));

                }

                setClipboardFile(fileList);

            }

        }).start();

Copying the file to the clipboard: 将文件复制到剪贴板:

public static void setClipboardFile(final List<File> files) {

    Transferable trans = new Transferable() {

        List<File> fileList = new ArrayList<File>();

        public DataFlavor[] getTransferDataFlavors() {

            for( File f: files ) {

                fileList.add(f);

            }

            return new DataFlavor[] { DataFlavor.javaFileListFlavor };

        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {

            return DataFlavor.javaFileListFlavor.equals(flavor);

        }   

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {

            if (isDataFlavorSupported(flavor))

                return fileList;

            throw new UnsupportedFlavorException(flavor);

        }

    };

    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);

}

What you're doing is simply wrong. 您在做什么完全是错误的。 Don't put something in the clipboard unless it's ready to be pasted -- you're breaking the contract that you should be following when using the clipboard. 除非已准备好将其粘贴,否则请勿在剪贴板中放置某些东西,否则会违反使用剪贴板时应遵循的约定。

Maybe my answer is a little bit simplistic, but why not simply wait until the file is completely downloaded before setting it in the clipboard? 也许我的回答有点简单,但是为什么不等到文件完全下载后再将其设置在剪贴板中呢? What about using something like a Future to wait for it, then set it in the clipboard. 使用诸如Future之类的东西等待它,然后在剪贴板中进行设置,该怎么办?

This... 这个...

fileList.add(new File("\\\\127.0.0.1\\r$\\downloaded" + value.replace("/", "\\")))

Is so wrong as to be mind boggling. 真是令人wrong目结舌。 You only want to add the where to the fileList but only AFTER it has been successfully downloaded. 您只想将where添加到fileList但是仅在成功下载之后。

Once the file has been downloaded, only then do you want to add it to the clipboard. 下载文件后,才要将其添加到剪贴板。

The creation of your Transferable is also a "little" troubling, the Transferable should make a copy of the File List the moment it is created, this prevents the List from been modified before it is used, which could change the results 创建Transferable也是一个“小”麻烦, Transferable应该在创建File List就对其进行复制,这样可以防止在使用列表之前对其进行修改,这可能会改变结果

In languages like C++ or Delphi it is possible to put to clipboard IDataObject object. 在像C ++或Delphi这样的语言中,可以将IDataObject对象放置到剪贴板中。 IDataObject object can contain absolutely different formats. IDataObject对象可以包含绝对不同的格式。 If your can paste IDataObject with CFSTR_FILEDESCRIPTOR and CFSTR_FILECONTENTS formats to clipboard your problem will be solved. 如果可以将具有CFSTR_FILEDESCRIPTORCFSTR_FILECONTENTS格式的IDataObject粘贴到剪贴板,则将解决您的问题。

When shell requests files from clipboard it checks for CF_HDROP, CF_IDLIST, CF_FILEDESCRIPTORW/CF_FILEDESCRIPTORA or CF_FILENAMEW/CF_FILENAMEA formats. 当Shell从剪贴板请求文件时,它将检查CF_HDROP,CF_IDLIST,CF_FILEDESCRIPTORW / CF_FILEDESCRIPTORA或CF_FILENAMEW / CF_FILENAMEA格式。 And if clipboard contains CF_FILEDESCRIPTOR format only shell will use this format for file transfer. 并且如果剪贴板包含CF_FILEDESCRIPTOR格式,则只有外壳程序将使用此格式进行文件传输。

CF_FILEDESCRIPTOR format contains names, attributes and dates of files. CF_FILEDESCRIPTOR格式包含文件的名称,属性和日期。 CF_FILECONTENTS format contains IStream of file. CF_FILECONTENTS格式包含文件的IStream。 And you are absolutely free to create any implementation of IStream object. 而且您完全可以自由创建IStream对象的任何实现。 When shell call IStream.Read and request the part of file which is already downloaded just return this part of file. 当外壳程序调用IStream.Read并请求文件的已下载部分时,只需返回文件的这一部分即可。 But when shell call IStream.Read and request the part of file which is not downloaded yet - just wait and after the part is downloaded return it. 但是,当外壳程序调用IStream.Read并请求尚未下载的文件部分时,请稍等,待下载部分后将其返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM