0

我在网页中创建了一个小程序,但每当我运行它时,我都会得到这个:

Exception in thread "Thread-13" java.security.AccessControlException: access denied ("java.io.FilePermission" "defensebg.png" "read")
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(Unknown Source)
    at java.io.File.canRead(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at Defense.run(Defense.java:63)
    at java.lang.Thread.run(Unknown Source)

我该如何解决?

4

2 回答 2

3

Since I know a little about your applet and am convinced these images are an inherent part of the application, I will take a different tack to paulms4.

Forget the File instances. They are neither workable for this, nor necessary. Only a trusted applet can access a File, but even then, the only place that an applet can establish a File is one that points to locations on the file-system of the user's PC. Obviously the images for your applet are not available that way (OK - they might be in a browser cache, but that is no use to us).

It would be more typical (& easier) to access applet resource by URL. An URL can be established relative to the code-base or document-base of the applet. If the images are actually inside a Jar, they become an embedded resource - see the info. page as to how to gain an URL.

Most of the methods in the J2SE that load a 'read only' resource will accept File, URL or InputStream. I use URL most often for the generic utility of it. An URL can represent a web resource, a file on the local file system, or a resource buried deep inside a Jar file (whether or on the web or the local file-system).

于 2012-06-24T08:17:25.203 回答
2

问:我该如何解决这个问题?

A:呃——不要尝试从小程序读取客户端 PC 上的文件?

或阅读“政策文件”:

Java 小程序在安全的“沙箱”中运行。这是设计使然 - 为了最终用户的安全:

于 2012-06-24T04:36:50.323 回答