利用java实现一个简单的远程监控程序一般的远程监控软件都是用c或者c++等语言开发的,而使用java如何来实现相同的功能呢。首先我们先介绍一下一个简单的远程监控程序的实现原理。功能一,远程屏幕监视(1)必须要有监控端与被监控端,而且程序保持启动。(2)被监控端获取本机的屏幕截屏发图给监控端。(3)监控端在本地窗口中显示被监控端发送过来的图像。(4)(2)(3)步骤重复执行,这时在监控端即可实时监视到被监控端的桌面操作了。功能二,远程控制(1)必须要有监控端与被监控端,而且程序保持启动。(2)在监控端监视窗体上执行鼠标点击事件。(3)记录步骤(2)中的鼠标点击的坐标,及键值发送到被监控端。(4)被监控接受鼠标坐标,及键值,然后再本地屏幕上模拟同样的点击动作。OK,现在看下具体的java与语言是如何实现上述功能的。使用java语言要实现截屏的功能就要依靠java类库中的一个有趣的类java.awt.Robot类【俗称Java机器人】了功能一,远程屏幕监视//『客户端』抓取屏幕快照GuiCamera.javaJava代码1BufferedImagescreenshot=2(newRobot()).createScreenCapture(3newRectangle(0,0,(int)size.getWidth(),4(int)size.getHeight()));//『客户端』发送快照SendThread.javaJava代码5image=gc.snapShot();6//保存为临时文件7Filefile=newFile("temp.png");8FileOutputStreamfileout=newFileOutputStream(file);9ImageIO.write(image,"png",fileout);10fileout.close();1112//读取图像13FileInputStreamfileIn=newFileInputStream(file);14intlen=(int)file.length();1516//建立字节数组17byte[]buf=newbyte[len];18fileIn.read(buf,0,len);1920//发送21out.write(buf,0,len);22out.flush();2324//间隔500毫秒25Thread.sleep(500);//『监控端』接受图像,Snap.javaJava代码26publicvoidrun(){27while(flag){28byte[]buf=newbyte[102400];29try{3031imgStream=newBufferedInputStream(32socket.getInputStream());33imgStream.read(buf);34ImageIconicon=newImageIcon(Toolkit.35getDefaultToolkit().36createImage(buf));37lab.setIcon(icon);3839Filefile=newFile("1.jpg");40FileOutputStreamfileOut=newFileOutputStream(file);41fileOut.write(buf);42fileOut.close();4344repaint();45setVisible(true);46System.out.println("读取图象成功!");47}catch(Exceptionex){48ex.printStackTrace();49flag=false;50}51}52System.out.println("服务器停止");53}功能二,远程控制『监控端』记录鼠标操作Snap.javaJava代码5455//内部类,主要功能监听鼠标事件。记录坐标。56classkeyAdapetextendsKeyAdapter57{//键盘监听适配器58publicvoidkeyTyped(KeyEvente){5960if(e.getKeyChar()==27){//按ESC键61Object[]options={62"确定",63"取消"};64intn=JOptionPane.showOptionDialog(null,65"是否退出程序?",66"远程监控系统",67JOptionPane.OK_CANCEL_OPTION,68JOptionPane.QUESTION_MESSAGE,69null,//don'tuseacustomIcon70options,//thetitlesofbuttons71options[0]);72if(0==n){73System.exit(0);74}75}7677}78}798081publicvoidmouseClicked(MouseEvente){8283System.out.println("双击了鼠标");84intx=e.getX();85inty=e.getY();86if(tempSocket!=null){87newCommandMsg("2",tempSocket,x,y).start();88}89}9091publicvoidmousePressed(MouseEvente){92if(e.BUTTON1==MouseEvent.BUTTON1){93System.out.println("你按了鼠标左键~~~~~~~~~~~");94intx=e.getX();95inty=e.getY();96if(tempSocket!=null){97newCommandMsg("3",tempSocket,x,y).start();98}99}100}101102......103}『监控端』发送坐标Snap.javaJava代码104publicvoidrun(){105out.println(eventType+","+x+","+y);106out.flush();107}『客户端』获取鼠标坐标后,在本机相同坐标位置模拟一个鼠标点击操作Coop.javaJava代码108publicvoidrun(){109while(flag){110try{111Strings=in.readLine();112decode(s);113switch(method){114//这里的man实际也是Robot的一个实例。115case1:116man.mouseMove(x,y);117break;118case2:119man.mouseMove(x,y);120man.mousePress(InputEvent.BUTTON1_MASK);121man.mouseRelease(InputEvent.BUTTON1_MASK);122break;123case3:124man.mousePress(InputEvent.BUTTON1_MASK);125break;126case4:127man.mouseRelease(InputEvent.BUTTON1_MASK);128break;129default:130break;131}132133}catch(IOExceptionexe){134ThreadInfo.CoopIsLive=false;135flag=false;136exe.printStackTrace();137}138}139}代码的部分就介绍到这里,由于java语言的一些限制,本实例仅作为演示。有感兴趣的朋友可以下载附件中的程序做进一步参考。java远程监控.rar(224.7KB)