Java实现远程控制技术完整源代码分享
java实现远程控制技术
java自带的java.net.和java.awt.robot. 的混合可以用于实现通过网络对另一台计算机的远程控制,其中包括控制远程计算机鼠标的动作和键盘的输入,以及实时获得远程计算机屏幕的图像。本文将用简洁的语言和由浅入深的逻辑,教大家如何掌握这个技术。
首先先看一下效果图:
远程端计算机界面:
控制端计算机界面:
控制端输入:
远程端输入:
一下开始详细介绍远程控制的技术思路。
首先两台计算机通过java.net的socket来进行连接。
一端先打开一个serversocket,然后另外一端用socket进行连接。
服务器端
应该设置一个serversocket,并且初始化需要用到的输入输出流:
public static void openserver() throws ioexception, classnotfoundexception{ system.out.println("serverstart....."); serversocket server = new serversocket(7777); socket = server.accept(); system.out.println("连接上...\n"+socket); ois = new objectinputstream(socket.getinputstream()); oos=new objectoutputstream(socket.getoutputstream()); }
客户机端
应该用socket去连接服务器,并且初始化输入输出流:
public static void startconnection(string ip,int port) throws unknownhostexception, ioexception, awtexception{ socket = new socket("192.168.0.106",7777); if(socket.isconnected()){ system.out.println("socket connected..."+socket); } oos = new objectoutputstream(socket.getoutputstream()); ois = new objectinputstream(socket.getinputstream()); }
这样两台计算机就链接在一起并且可以通过流(inputstream和outputstream)来交换数据了
接下来大家可以想一想,要实现远程控制的两台计算机需要交换什么信息呢?首先被控制端需要不断向控制端提供截取的屏幕图像(这个我们将会用java.awt.robot来实现),然后鼠标和键盘根据控制端传来的事件(inputevent)来做出相同的操作(用robot来实现)。然后控制端当然首先要接收被控制端传来的图像并且反映到一个面板上(pane),然后监听本机上键盘鼠标的动作再传给被控制端的主机(我们通过在面板pane上设置一个监听器listener来实现)
这里遇到的一个问题就是用于传送的图片无论是用image还是用bufferedimage都是不可串行化的。所以不能用i/ostream进行传送,所以为了解决这个问题,我们需要把图像数据封装在一个类里面并implements serializable接口
图像类如下:
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.serializable; public class message implements serializable { private static final long serialversionuid = 1l; private string filename; // 文件名称 private long filelength; // 文件长度 private byte[] filecontent; // 文件内容 public message(){ } public message(string filepath) throws ioexception{ file file = new file(filepath); this.filelength=file.length(); this.filename=file.getname(); fileinputstream fis = new fileinputstream(filepath); byte[] bytes = new byte[(int)filelength]; fis.read(bytes,0,(int)filelength); this.filecontent=bytes; } public string getfilename() { return filename;} public void setfilename(string filename) { this.filename = filename;} public long getfilelength() { return filelength; } public void setfilelength(long filelength) {this.filelength = filelength;} public byte[] getfilecontent() {return filecontent;} public void setfilecontent(byte[] filecontent) {this.filecontent = filecontent;} }
这样就可以实现图像通过objectinputstream和objectoutputstream的串行化传播了
了解了以上基础之后首先我们要完成控制端的ui界面设置,图片接收,和键盘鼠标动作监听:
首先是设置接收图片:
public static void reveivepic() throws classnotfoundexception, ioexception{ message g = (message)ois.readobject(); fileoutputstream fos = new fileoutputstream("d:\\out\\"+g.getfilename()); fos.write(g.getfilecontent(),0,(int)g.getfilelength()); fos.flush(); fileinputstream fis= new fileinputstream("d:\\out\\"+g.getfilename()); bufferedimage bi = imageio.read(fis); iic=new imageicon(bi); image img = iic.getimage(); toolkit tk = toolkit.getdefaulttoolkit() ; dimension d =tk.getscreensize(); int w = d.width; int h =d.height; bufferedimage bi = resize(img,800,600); imag_lab.seticon(new imageicon(bi)); imag_lab.repaint();//销掉以前画的背景 } private static bufferedimage resize(image img, int neww, int newh) { int w = img.getwidth(null); int h = img.getheight(null); bufferedimage dimg = new bufferedimage(neww, newh,bufferedimage.type_int_bgr); graphics2d g = dimg.creategraphics(); g.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g.drawimage(img, 0, 0, neww, newh, 0, 0, w, h, null); g.dispose(); return dimg; }
这样接收了来自objectinputstream的message类之后就可以把图片重新设置到面板pane的大小然后展示出来
下一步就是设置面板属性和监听器:
public static void showui(){ //控制台标题 jframe jf = new jframe("控制台");setlistener(jf); //控制台大小 jf.setsize(500, 400); //imag_lab用于存放画面 imag_lab = new jlabel(); jf.add(imag_lab); //设置控制台可见 jf.setvisible(true); //控制台置顶 jf.setalwaysontop(true); jf.setresizable(true); jf.setdefaultcloseoperation(jframe.exit_on_close); }
监听器:
public static void setlistener( jframe frame){ //panel设置监听器 frame.addkeylistener(new keyadapter(){ public void keypressed(keyevent e) { sendeventobject(e); } @override public void keyreleased(keyevent e) { sendeventobject(e); } @override public void keytyped(keyevent e) { } }); frame.addmousewheellistener(new mousewheellistener(){ public void mousewheelmoved(mousewheelevent e) { sendeventobject(e); } }); frame.addmousemotionlistener(new mousemotionlistener(){ public void mousedragged(mouseevent e) { sendeventobject(e); } public void mousemoved(mouseevent e) { sendeventobject(e); } }); frame.addmouselistener(new mouselistener(){ public void mouseclicked(mouseevent e) { sendeventobject(e); } public void mouseentered(mouseevent e) { sendeventobject(e); } public void mouseexited(mouseevent e) { sendeventobject(e); } public void mousepressed(mouseevent e) { sendeventobject(e); } public void mousereleased(mouseevent e) { sendeventobject(e); } }); } private static void sendeventobject(inputevent event){ try{ system.out.println("send"); oos.writeobject(event); oos.flush(); }catch(exception ef){ ef.printstacktrace(); }
以上就完成了控制端。
接下来我们将构建被控制端:
被控制端需要使用robot来截图并发送,而且需要写一个方法来对接收到的inputevent进行反应
首先是截图和发送:
public static void capturepic() throws awtexception, ioexception{ robot= new robot(); message msg = null; toolkit tk = java.awt.toolkit.getdefaulttoolkit(); java.awt.dimension dm =tk.getscreensize(); java.awt.robot robot = new java.awt.robot(); for (int i = 0; i < 50; i++) { //截取指定大小的屏幕区域 rectangle rec = new rectangle(0, 0, (int) dm.getwidth(), (int) dm .getheight()); bufferedimage bimage = robot.createscreencapture(rec); //将图片保存到文件中 string filepath = "d:\\out\\screenshot"+i+".jpeg"; fileoutputstream fops =new fileoutputstream(filepath); javax.imageio.imageio.write(bimage, "jpeg", fops); fops.flush(); fops.close(); msg =new message(filepath); system.out.println(msg.getfilename()); system.out.println("send"); oos.writeobject(msg); oos.flush(); } }
注意到这段代码中使用了d:\out\目录作为临时文件的存放地方,读者使用这个代码的时候需要自己设置临时文档的存放
然后实现robot对于接收到的inputevent指令进行操作:
public void action() throws awtexception, classnotfoundexception, ioexception{ robot robot= new robot(); while(true){ inputevent e =(inputevent)ois.readobject(); if(e!=null){ handleevents(robot,e);} } } public static void handleevents(robot action,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 static int getmouseclick(int button) { //取得鼠标按键 if (button == mouseevent.button1) //左键 ,中间键为button2 return inputevent.button1_mask; if (button == mouseevent.button3) //右键 return inputevent.button3_mask; return -100; }
整个程序到这里就可以结束了。上面的程序并没有实现对机器人类线程的封装。完整可以用的代码可以在以下站内资源处下载我的资源:http://xiazai.jb51.net/201608/yuanma/java-romotecontrol(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读