Java JDK1.4 的Robot对象,该对象可以完成屏幕图像截取操作,控制鼠标,键盘,如此便可以轻而易举地实现远程服务器的控制。本文向大家介绍如何用Java Robot对象实现远程服务器的控制,并给出了相应的Java源代码。
实现原理:

   (1)被控端通过Robot截取桌面图像通过socket通信传给主控端。
   (2)主控端显示被控端桌面,并且把基于被控端图像上的键盘,鼠标事件通过socket通信传给被控端。
    (3)被控端响应主控端的鼠标键盘事件。
实现详解:

    (一)主控端和被控端通过UDP进行指令交互
         /*-主控端向被控端发送指令-*/
        String command = …;   //指令
        InetAddress inet = …; //被控端地址
        int port = …;         //端口
        byte sp[] = command.getBytes();
         DatagramPacket packet = new DatagramPacket(sp,sp.length(),inet,port);
        DatagramSocket sd = new DatagramSocket();
        sd.send(packet);
        /*-被控端接收指令-*/
        DatagramSocket stockUDP = …;
        DatagramPacket packet = …;
         stockUDP.receive(packet);
         String message=new String(packet.getData(),0,packet.getLength()); //得到指令  
        本程序中用到的指令
        public static final String Command_Connection = “connection” ;      //建立连接
        public static final String Command_Disconnection = “disconnection”;//断开连接
        public static final String Command_Screen = “screen”;               //显示被控端屏幕
        public static final String Command_Control=”control”;              //建立控制套接
   (二)被控端向主控端传递图像
        /*-被控端获取屏幕图像-*/
        Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());//全屏区域
        Robot robot=new Robot();
        BufferedImage image = robot.createScreenCapture(rect);
        /*-开启图像传送线程-*/
        Socket socket = …;
        OutputStream out = socket.getOutputStream();
        BufferedImage image ;
        JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
        ……
        public void run(){     
            while(true){
               image = …; //获取屏幕图像
                encoder.encode(image); //发送图像给主控端
                Thread.sleep(Environment.IMAGE_GETTIME); //图像采集时间间隔
            }
        }
        /*-主控端图像接收线程-*/
        JPEGImageDecoder de = JPEGCodec.createJPEGDecoder(socket.getInputStream());
        BufferedImage image = null ;
        ……
        public void run(){
            while(true){
                image = de.decodeAsBufferedImage();
                if (image != null) {
                     /*显示图像*/
                }
            }
        }
   (三)图像显示,鼠标,键盘监听
        /*-图像显示-*/
        public class ControlledScreenPanel extends JPanel implements MouseMotionListener,
                                          MouseListener,MouseWheelListener ,KeyListener{
            private BufferedImage image ;
            public void setBufferedImage(BufferedImage bi) {   //更新图像
                image = bi;
                Dimension d = new Dimension(image .getWidth(this),image .getHeight(this));
                setPreferredSize(d);
                revalidate();
                repaint();
            }
            public void paintComponent(Graphics g) {   //绘制图像
                super.paintComponent(g);
                Dimension d = getSize();
                g.setColor(getBackground());
               g.fillRect(0, 0, d.width, d.height);
               if (image != null) {
                   g.drawImage(image , 0, 0, this);
                }
            }
        }
        /*-鼠标,键盘监听-*/
        private MainControlSocket control ; //发送鼠标,键盘事件到被控端
        public void mouseDragged(MouseEvent e) {
            control.sendControlledAction(e);
        }
        public void mouseMoved(MouseEvent e) {
            control.sendControlledAction(e);
        }
        //————————————————————————–
        public void mouseClicked(MouseEvent e) { 
            requestFocus();//单击获得焦点
        }
        public void mousePressed(MouseEvent e) { //鼠标按下
            control.sendControlledAction(e);
        }
        public void mouseReleased(MouseEvent e) { //鼠标释放
            control.sendControlledAction(e);
        }
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        //————————————————————————–
        public void mouseWheelMoved (MouseWheelEvent e){ //滑轮滚动
             control.sendControlledAction(e);
        }
        //————————————————————————–
        public void keyTyped(KeyEvent e) {}
        public void keyPressed(KeyEvent e) { //键盘按下
            control.sendControlledAction(e);
        }
        public void keyReleased(KeyEvent e) { //键盘释放
            control.sendControlledAction(e);
       }
        /*-发送鼠标,键盘事件到被控端-*/
       class MainControlSocket {
            private Socket socket = null;
            private ObjectOutputStream out = null; //事件对象发送封装
            ……
            public void sendControlledAction(InputEvent event) {
                try {
                 out.writeObject(event);
               }
               catch (IOException e) {
                  e.printStackTrace();
               }
            }
            ……
       }
   (三)被控端接收,响应键盘鼠标事件
        private Socket socket = …;
        private ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        private Robot action = new Robot();

         public void run(){
            ……
            while(true){
                Object obj = in.readObject();       //获得鼠标键盘事件
                if (obj != null) {
                    handleEvent( (InputEvent) obj); //处理鼠标键盘事件

                }
            }
            ……
        }      
        private void handleEvent(InputEvent event){
            MouseEvent mevent = null ; //鼠标事件
            MouseWheelEvent mwevent = null ;//鼠标滚动事件
            KeyEvent kevent = null ; //键盘事件
            int mousebuttonmask = -100; //鼠标按键
            switch (event.getID()){
                case MouseEvent.MOUSE_MOVED :                       //鼠标移动
                    mevent = ( MouseEvent )event ;
                    action.mouseMove( mevent.getX() , mevent.getY() );
                    break ;
                case MouseEvent.MOUSE_PRESSED :                      //鼠标键按下
                    mevent = ( MouseEvent ) event;
                    action.mouseMove( mevent.getX() , mevent.getY() );
                    mousebuttonmask = getMouseClick( mevent.getButton() );
                    if(mousebuttonmask != -100)
                        action.mousePress(mousebuttonmask);
                    break;
                 case MouseEvent.MOUSE_RELEASED :              //鼠标键松开
                    mevent = ( MouseEvent ) event;
                    action.mouseMove( mevent.getX() , mevent.getY() );
                    mousebuttonmask = getMouseClick( mevent.getButton() );//取得鼠标按键
                    if(mousebuttonmask != -100)
                        action.mouseRelease( mousebuttonmask );
                    break ;
                case MouseEvent.MOUSE_WHEEL :                  //鼠标滚动
                    mwevent = ( MouseWheelEvent ) event ;
                    action.mouseWheel(mwevent.getWheelRotation());
                    break ;
                 case MouseEvent.MOUSE_DRAGGED :                      //鼠标拖拽
                    mevent = ( MouseEvent ) event ;
                    action.mouseMove( mevent.getX(), mevent.getY() );
                    break ;
                 case KeyEvent.KEY_PRESSED :                     //按键
                    kevent = ( KeyEvent ) event;
                    action.keyPress( kevent.getKeyCode() );
                    break ;
                 case KeyEvent.KEY_RELEASED :                    //松键
                    kevent= ( KeyEvent ) event ;
                    action.keyRelease( kevent.getKeyCode() );
                    break ;
                default: break ;
            }
        }
        private int getMouseClick(int button) {    //取得鼠标按键
            if (button == MouseEvent.BUTTON1) //左键 ,中间键为BUTTON2
                return InputEvent.BUTTON1_MASK;
            if (button == MouseEvent.BUTTON3) //右键
                return InputEvent.BUTTON3_MASK;
            return -100;
        }
        ……
     }

 

http://blog.csdn.net/songylwq/article/details/7715157

 

版权声明:本文为softidea原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/softidea/p/4159161.html?ivk_sa=1024320u