写了一个servlet ,处理逻辑是:客户端带文本参数请求服务器的servlet,servlet获取信息后,将信息复制到服务器剪切板,打开微信,粘贴发送(该操作均为后端服务器)
整个流程在IDEA中运行(通过tomcat)能够正确动作,但是单独在TOMCAT服务器上运行确无法正确动作,是因为要操作服务器的电脑窗口或者剪切板有什么权限限制么?
```java
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.awt.Desktop;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.awt.datatransfer.Transferable;
@WebServlet(name = "clb", value = "/clb")
public class CLBset extends HttpServlet {
public void init(){
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Desktop deskapp = Desktop.getDesktop();
if(Desktop.isDesktopSupported()){
try {
deskapp.open(new File("D:\\Tencent\\WeChat\\WeChat.exe"));
} catch (IOException e) {
e.printStackTrace();
}
}
Robot robot = null;
//
//获取剪切板
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
//将传入字符串封装下
Transferable tText = new StringSelection("请注意:@王二麻子 新任务:" + new Date());
//将字符串放入剪切板
clip.setContents(tText, null);
//
try {
robot = new Robot();
robot.delay(100);//可以控制发送间隔时间
//点击鼠标左键(目的是让光标放到vx上)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(100);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(100);
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
}
```