• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception loading images

 
Chris Robinson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have built an applet that has a few buttons on it and everything was working fine. However, when I tried changing the buttons to use icons made from gif images I get something like:

AccessControlException: access denied (java.io.FilePermission ..... read)

Now, I understand that this is a security issue to protect the clients system from malicious access to their machine but what I don't get is that I am trying to access images that are in the same directory as the applet classes, not client files. I don't want to create or change any files on the local machine. Is there a simple way around this by using certain pathname conventions or something. At the moment I am just using the image filename since all the files are in one directory.
Any help greatly appreciated.
Chris.
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to read the files from the local file system. Imagine your client is on one computer and your web server is on another. How would you find the image files when they're on the server's file system?
 
Chris Robinson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
Imagine your client is on one computer and your web server is on another. How would you find the image files when they're on the server's file system?


This is why I don't understand the problem. If the client downloads an applet class from the server then it must know the address that it came from. Therefore if I keep all the image files in the same directory as the applet class why should there be a problem if I try to reference the files using purely the filename and no path?
I also tried using the full URL of the image on the server but then I get a Socket Permission error at the target URL.
I just want to put my own icons on some buttons on my applet. Why is there a security issue trying to do this and what is the easiest way to rectify the problem?
[ March 28, 2005: Message edited by: Chris Robinson ]
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Robinson:

This is why I don't understand the problem. If the client downloads an applet class from the server then it must know the address that it came from.


You can FIND the address it came from (through Applet.getDocumentBase() or Applet.getCodeBase()) but that doesn't mean that all file system requests automatically get converted into network requests. Code does what you tell it to do. A FileInputStream ALWAYS accesses the file system. If you construct a URL you have to get the host address that you want to access and feed it to the URL constructor. Applets are only permitted to contact the host they were downloaded from, so plugging anything else in there will cause a security exception. If this doesn't make sense, share a sample of your code trying to load an image from a URL and we'll work through it.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your applet code, did you use getImage or ImageIO?
 
Chris Robinson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Problem solved.
I changed my code to use Applet.getCodeBase() to create the URL object as you suggested and it all works fine.
Thanks for your help.
Chris.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic