Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File read permission error for an applet

Status
Not open for further replies.

sdumitru

Programmer
May 8, 2005
3
RO
I have the following problem: I cannot get an applet to read from a file, even though it is located in the same directory (the ROOT directory of the Tomcat installed by the J2SDK 1.4.2_04).
It has something to do with Tomcat permissions, because I get the following error: "java.security.AccessControlException: access denied (java.io.FilePermission fileToRead.txt read)".
I tried to edit the "catalina.policy" file in the "%CATALINA_HOME%\conf" directory to set "AllPermission" to the applet's class, I also tried to start Tomcat with "#-security" option as suggested in a post on this forum. Nothing worked.
When I load the page containing the applet from the local filesystem, not through the web-server (at the address "localhost:8080/test.html"), the applet reads the file and outputs it's content just fine (I wrote a simple applet that does nothing but read the file and output it's content in a TextArea, to be able to determine where the problem is, why am I not allowed to read files in my more complex applet). If the file is in a subdirectory, I also get an error about permission to read "user.dir", if I specify the path to the file by a "subdirectory\\file.txt" String.

This must be a trivial issue, but I am only a beginner in terms of web-server usage and web-programming. I mostly wrote applications for my local system. If someone would find the time to reply, I would greatly appreciate the help.
 
First of all, thank you for your quick response, sedj!
I read about signed applets, but this seems to be a little too complicated for what I have to do.
I don't want to access the local file system on the computer on which the browser is running, all I need is to read a file from the directory in which the applet is stored back on its server.
From what I read everywhere, this should be granted in standard mode by the server. Maybe there is a problem with the way in which I specify the PATH to the file? The String filename that I pass to "File f = new File(fileName);" should contain perhaps the URL to the file back on the server? The "java.io.File" class contructor does not support URL arguments though...
As I stated in my original message, I only pass the name of the file, having in mind the fact that the file is in the same directory as the applet.
 
Remember though that applets run on the client machine - not the server - so when you say :

File f = new File("whateber");

... this is is attempting to access the file system that the applet is running under - ie the client, not the server.

--------------------------------------------------
Free Database Connection Pooling Software
 
I'm sorry I wasted your time with this post. I found out the way to do the reading of the file on the server, the code was very simple, as I expected:

Code:
URL fileURL = null;
InputStream in;

try {
	fileURL = new URL("[URL unfurl="true"]http://IPAddress:8080/fileToRead");[/URL]
} catch (MalformedURLException e) {
	System.out.println("Malformed URL");
}

try {
	in = fileURL.openConnection().getInputStream();
} catch (Exception e) {
	System.out.println(e);
	e.printStackTrace();
}

After this I use the in InputStream for reading.
For granting permission to the applet to do the reading, both signing the jar containing the applet and editing the catalina.policy file of the Tomcat server to grant AllPermission to the class worked, after (silly me! :-D) configuring my firewall to grant me access to my files through the Internet! (This was why I still got the error even though I signed the applet...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top