一步步实现一个“云粘贴板”:再搞一个PC端

一步步实现一个“云粘贴板”:再搞一个PC端

PC端需要实现两个功能,一监听系统复制事件,把文本内容同步到服务端。二监听服务端内容变动,把服务端内容复制到系统粘贴板中。


前言

选什么语言来开发这个web服务呢?
本着拿来主义原则,百度搜索“监听系统粘贴板变化”,第一条居然是基于java的jwt实现的,就他了!


一、开始Coding…

写个程序SystemClipboardMonitor.java:

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLDecoder;


public class SystemClipboardMonitor implements ClipboardOwner {
    private static String LAST_REMOTE = "";
    private static final String SERVER_URL = "http://localhost:8888/clipboard/";
    private static Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    private static SystemClipboardMonitor THIS;

    public SystemClipboardMonitor() {
        if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            clipboard.setContents(clipboard.getContents(null), this);
        }
    }

    public static void main(String[] args) {
		System.out.println(get(SERVER_URL));
        THIS = new SystemClipboardMonitor();
        new Thread(() -> {
            while (true) {
                try {
                    String remote = URLDecoder.decode(get(SERVER_URL), "utf-8");
                    if (!LAST_REMOTE.equals(remote)) {
						System.out.println("云端复制:" + remote);
                        clipboard.setContents(new StringSelection(remote), THIS);
                        LAST_REMOTE = remote;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        JFrame frame = new JFrame();
		frame.setVisible(true);
		frame.setExtendedState(JFrame.ICONIFIED);
    }

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String text = null;
        if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            try {
				text = (String) clipboard.getData(DataFlavor.stringFlavor);
				if (!LAST_REMOTE.equals(text)) {
					LAST_REMOTE = text;
					System.out.println("本地复制:" + text);
					text = URLEncoder.encode(text, "utf-8");
					get(SERVER_URL + text);
				}
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        StringSelection tmp = new StringSelection(text);
        clipboard.setContents(tmp, this);
    }

    public static String get(String url) {
        StringBuffer stringBuffer = new StringBuffer();
        HttpURLConnection conn = null;
        InputStream is = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.connect();
            is = conn.getInputStream();
			int size = 0;
            byte[] buffer = new byte[64];
            while((size = is.read(buffer)) != -1){
                stringBuffer.append(new String(buffer, 0, size));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return stringBuffer.toString();

    }
}

运行一下看看:

# 编译
javac -encoding utf-8 SystemClipboardMonitor.java
# 运行
java SystemClipboardMonitor

二、第二步做什么呢

哪有什么第二步?已经搞好了,编程好累啊,赶紧去休息休息。

总结

不擅长总结,不写了