package com.company.app.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

@CrossOrigin
@RestController
public class DemoController {



    @GetMapping(value = "/get",produces = {"application/json; charset=UTF-8"})
    public String get() {
        return getClipboardString();
    }

    @GetMapping("/set")
    public String get(String value) {
         setClipboardString(value);
         return "ok:"+getClipboardString();
    }

    /**
     * 从剪贴板中获取文本(粘贴)
     */
    public static String getClipboardString() {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 获取剪贴板中的内容
        Transferable trans = clipboard.getContents(null);

        if (trans != null) {
            // 判断剪贴板中的内容是否支持文本
            if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    // 获取剪贴板中的文本内容
                    String text = (String) trans.getTransferData(DataFlavor.stringFlavor);
                    return text;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    /**
     * 把文本设置到剪贴板(复制)
     */
    public static void setClipboardString(String text) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(text);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }

    public static void main(String[] args) {
        String clipboardString = getClipboardString();
        System.out.println(clipboardString);
    }

}
 

 

package com.company.app;


import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
//        SpringApplication.run(Application.class, args);
        SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
        builder.headless(false).web(WebApplicationType.SERVLET).run(args);

    }
}

前端代码实现:

<!DOCTYPE html>
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<p type="text" id="myInput"></p>
<!-- <button id="copyButton">复制到剪切板</button> -->
</body>
<script>
// 创建一个 XMLHttpRequest 对象
var xhr = new XMLHttpRequest();

// 设置请求的 URL
xhr.open('GET', 'http://10.64.153.130:19000/get');

// 设置请求头,包括 Content-Type 和 Accept 等信息
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');

// 发送请求并获取响应
xhr.onload = function() {
if (xhr.status === 200) {
// 获取响应数据
// var data = JSON.parse(xhr.responseText);
var data=xhr.responseText;

// 将数据绑定到 DOM 元素上
document.getElementById('myInput').innerHTML = data;
navigator.clipboard.writeText(data).then(() => {
console.log('文本已复制到剪切板');
}).catch(() => {
console.error('无法复制文本到剪切板');
});
// document.getElementById('myInput').innerHTML = data.age + '岁';
} else {
console.log('There was a problem with the fetch request.');
}
};

// 发送请求
xhr.send();
</script>
</html>

 

https://www.ruanyifeng.com/blog/2021/01/clipboard-api.html

posted on 2023-01-28 14:44  张释文  阅读(40)  评论(0编辑  收藏  举报