java.security.AccessControlException异常一般怎么处理
写的一个Applet小程序,在读入一个图片时,出现了java.security.AccessControlException异常,怎么处理。
Exception in thread "Thread-3" java.security.AccessControlException: access deni
ed ("java.io.FilePermission" "\D:\java\javaAppletExample\ch2\html\Sunset.gif" "r
ead")
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:366)
at java.security.AccessController.checkPermission(AccessController.java:
555)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
at sun.awt.image.URLImageSource.<init>(URLImageSource.java:55)
at sun.applet.AppletImageRef.reconstitute(AppletImageRef.java:51)
at sun.misc.Ref.get(Ref.java:64)
at sun.applet.AppletViewer.getCachedImage(AppletViewer.java:395)
at sun.applet.AppletViewer.getImage(AppletViewer.java:390)
at java.applet.Applet.getImage(Applet.java:274)
at java.applet.Applet.getImage(Applet.java:296)
at Ripple.run(Ripple.java:47)
at java.lang.Thread.run(Thread.java:722)
源程序47行是:
image = getImage(getDocumentBase(),name);
------解决方案--------------------因为浏览器只能访问一定目录下的文件,不是可以访问所有的文件,Applet没有那么高的权限。所以解决方案有两个:
1,通过签名来解决:
2,通过更改java/lib/security/java.?policy文件来实现。当然这种方式肯定不安全。具体做法是:将下面内容添加到java.policy文件中:
? permission java.util.PropertyPermission "user.dir ", "read ";
permission java.util.PropertyPermission "user.home ", "read ";
permission java.util.PropertyPermission "java.home ", "read ";
permission java.util.PropertyPermission "java.class.path ", "read ";
permission java.util.PropertyPermission "user.name ", "read ";
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc ";
permission java.lang.RuntimePermission "accessClassInPackage.sun.audio ";
permission java.lang.RuntimePermission "modifyThread ";
permission java.lang.RuntimePermission "modifyThreadGroup ";
permission java.lang.RuntimePermission "loadLibrary.* ";