Java Applet can't load images from web

痞子三分冷 提交于 2019-12-25 05:19:03

问题


I have wrote my first java applet.

It basically loads some images from my Django webserver and then the user can modify them.

I developed it in Eclipse, and I had no problems at all. When I tried it with a test page, on the java console come out this error:

java.security.AccessControlException: access denied (java.net.SocketPermission www.hyros.net resolve)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1031)
    at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Applet2SecurityManager.java:500)
    at sun.plugin2.applet.Plugin2Manager$AppletContextImpl.getImage(Plugin2Manager.java:2718)
    at java.applet.Applet.getImage(Applet.java:242)
    at MapGenerator.getResourceImage(MapGenerator.java:50)
    at MapGenerator.init(MapGenerator.java:35)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1637)
    at java.lang.Thread.run(Thread.java:662)
Eccezione: java.security.AccessControlException: access denied (java.net.SocketPermission www.hyros.net resolve) 

The incriminated piece of code:

for(int i=0; i< numero_immagini; i++) {
    try {
        URL url = new URL(this.getParameter(IMMAGINE+i));
        images[i] = ImageIO.read(url);
        floors[i] = Integer.parseInt(this.getParameter(PIANO_IMMAGINE+i));
    } catch (IOException ioe) {ioe.printStackTrace();}
}

For the test i tried an image from my local webserver, but the error comes out with every possible link, internal or external.

Thank you in advance.


回答1:


Unless the applet is signed, it will not be able to access images at a different location other then that of the originating server. Here you could use:

images[i] = getImage(getDocumentBase(), IMMAGINE + i);



回答2:


Ok, Solved it!

The line of code to change is

URL url = new URL(this.getParameter(IMMAGINE+i));

to

URL url = new URL(getCodeBase(), this.getParameter(IMMAGINE+i));


来源:https://stackoverflow.com/questions/12624572/java-applet-cant-load-images-from-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!