小编典典

使用java.awt.Robot类在applet中进行屏幕捕获时,java.security.AccessControlException

java

每当客户端单击打印屏幕按钮时,我都需要捕获一个网页屏幕以将其存储在客户端计算机上。为此,我通过在jsp页面中嵌入带有签名的小程序(受信任的小程序)来进行搜索。因此,我正在尝试为独立的Java类使用一个简单的applet。成功后,我可以在签署了applet后尝试使用jsp。我试过的是:

import java.applet.Applet;  
import java.awt.Graphics;  
import java.util.Date;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
/* <applet code = MyApplet.class width="500" height="500">Java Applet for screen capture</applet>  */



public class MyApplet extends Applet {

  /* Applet Life cycle Methods */  
  public void start()
  { 
   try{
   // capture the whole screen
   BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
   // Save as JPEG
   File file = new File("D:/screencapture.jpg");
   ImageIO.write(screencapture, "jpg", file);
   System.out.println("screen capture finished : ");
     }//try closing...
     catch(Exception e)
     {
       System.out.println("screen capture error : ");
       e.printStackTrace();
     }//catch closing...  
   }//start closing...

 public void stop()
  {

  }   
}

我得到这个:

java.security.AccessControlException: access denied ("java.awt.AWTPermission" "createRobot")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
        at java.security.AccessController.checkPermission(AccessController.java:560)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at java.awt.Robot.checkRobotAllowed(Robot.java:170)
        at java.awt.Robot.init(Robot.java:134)
        at java.awt.Robot.<init>(Robot.java:96)
        at MyApplet.start(MyApplet.java:23)
        at sun.applet.AppletPanel.run(AppletPanel.java:474)
        at java.lang.Thread.run(Thread.java:722)

任何帮助,任何想法都会得到应用。


阅读 250

收藏
2020-11-30

共1个答案

小编典典

我解决了这个问题。我所做的只是将这些行粘贴到java.policy文件中(只需在Java安装文件夹中搜索此文件,您将在3个位置得到它,最后需要将其粘贴到所有文件中)

permission java.awt.AWTPermission "createRobot"; 
permission java.awt.AWTPermission "accessClipboard"; 
permission java.awt.AWTPermission "accessEventQueue"; 
permission java.awt.AWTPermission "showWindowWithoutWarningBanner"; 
permission java.awt.AWTPermission "readDisplayPixels", "read"; 
permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute";
2020-11-30