简体   繁体   English

XPages-使用Java将文本复制到本地剪贴板

[英]XPages - Using Java to copy text to local clipboard

I'm trying to use Java code in an XPages application to copy text to the local Windows clipboard. 我正在尝试在XPages应用程序中使用Java代码将文本复制到本地Windows剪贴板。 My code (executed by clicking a button) copies the code and outputs it to the IBM / Lotus Domino server console. 我的代码(通过单击按钮执行)将代码复制并输出到IBM / Lotus Domino服务器控制台。 I would like to be able to paste the copied text locally (on my Windows 7 PC) using Ctrl-V but this does not work. 我希望能够使用Ctrl-V在本地(在Windows 7 PC上)粘贴复制的文本,但这不起作用。 What change does my code need to make this work? 为了使这项工作有效,我的代码需要进行哪些更改?

import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.io.*;

        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();

        StringSelection testData;

        //  Add some test data 

        testData = new StringSelection( "New Test Data" );

        c.setContents(testData, null);

        //  Get clipboard contents, as a String

        Transferable t = c.getContents( null );

        if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
        {
            Object o = t.getTransferData( DataFlavor.stringFlavor );
            String data = (String)t.getTransferData( DataFlavor.stringFlavor );
            System.out.println( "Clipboard contents: " + data );
        }

        System.exit(0);

This is not possible with Java. 对于Java,这是不可能的。 The Java code runs on server and has no possibilities to "fill" client's clipboard. Java代码在服务器上运行,无法“填充”客户端剪贴板。

If you don't depend on IE or older browser versions then use HTML5 Clipboard API in CSJS. 如果您不依赖IE或更旧的浏览器版本,请在CSJS中使用HTML5剪贴板API ( https://stackoverflow.com/a/26336421/2065611 http://datatables.net/blog/2014-01-31#Clipboard-API ). https://stackoverflow.com/a/26336421/2065611 http://datatables.net/blog/2014-01-31#Clipboard-API )。 Get the text for clipboard from server with XSP.partialRefreshGet and execute ClipboardEvent 'copy' onComplete. 使用XSP.partialRefreshGet从服务器获取剪贴板的文本,并在onComplete上执行ClipboardEvent'copy'。

In addition to Knut Herrmanns answer: it is not possible to copy text to the user's clipboard using for instance client-side Javascript. 除了Knut Herrmanns回答之外:例如,不可能使用客户端Javascript将文本复制到用户剪贴板。

However, you can use Flash for this. 但是,您可以为此使用Flash。 I have used ZeroClipboard for this in a project. 我在项目中为此使用ZeroClipboard

Update: Knut updated his answer with a reference to the HTML5 Clipboard API. 更新: Knut通过引用HTML5剪贴板API更新了他的答案。 So my answer is only needed if you need to support IE and older browsers - and who isn't :-) 因此,仅当您需要支持IE和较旧的浏览器时才需要我的答案-谁不支持:-)

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

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