Click to See Complete Forum and Search --> : Applet can read file?


salim
November 24th, 1999, 10:19 AM
Hello,
how can an applet read a file? What happen with Applet SecurityException? I am curious and i wish i could do it.

DHunter21
November 24th, 1999, 05:13 PM
An applet can not read a file or write to files except for cookies. This is to secure your private files and information from hackers. The applet can only get information from the server from which it was loaded from, and a resident cookie.

Dustin

Leon
November 26th, 1999, 01:59 PM
You will have to sign the applet
That's the only way to access some resources on the client's mashine
...u will face some difficultes with it :(

Leon
http://people.bulgaria.com/vleonkev

December 22nd, 1999, 01:04 AM
If an applet cannot read a file then how can it load an image onto itself,for example?
That will require opening a .jpeg file,how is that allowed?
Actually this was asked to me in an interview.I lost the offer.

DHunter21
December 22nd, 1999, 12:36 PM
An applet can ONLY open a file that is sent from the same place the applet resides if the applet is trusted then it can open from several other places like your hard-drive. That means if you run an applet at www.codeguru.com/java/MyApplet.class, it can open all the files in that directory (and sub directories I believe) that it likes.That is because this is a secure connection. The signed applet is a different matter. You explicitly allow the applet to access to your hardrive, usually to store files. Designing a signed applet is not a simple procedure to accomplish.


Hope it help... better luck on the next job
Dustin

gangelo
January 6th, 2000, 01:12 PM
Does this apply to Digitally Signed Applets as well?

salim
January 6th, 2000, 03:58 PM
When applet is signed? Do you mean use policytool to sign it? I wrote an applet before.This applet will read a file from the same directory on Server's harddrive. Even i signed the applet using policytool, i still get the same problem saying security exception? Is this problem can be overcome? I solve this problem by encoding the data file on the applet itself. But i know this is not a good practice since it is inefficient for multiple big files.

salim
January 6th, 2000, 10:37 PM
I don't know how to read the file from the same directory the applet come from. Can you show me using a shor example?

Let me use Hello World applet as and example. Let say i have a file call Hello.txt which contains a string "Hello World" only. I write and applet which will read this "Hello World" string and display it. Let my applet called MyHelloWorld.java. I this applet i put a button which will response to the press action on it. When the button is pressed, I show the "Hello World" string. I compile it. Then i write a simple HelloWorld.html to run my MyHelloWorld.class. Then i save these three files in the same directory. After that i open netscape to view the HelloWorld.html. Can i see "Hello World"?

I encounter the problem to read a file using applet. I use BufferedReader cascade with FileReader to instanciate and object. I get AppletSecurityException all the time.
If you have any suggestion, please help me up. Thanks.

Chris Allen
January 7th, 2000, 06:47 AM
When testing an applet it is not a good idea to read it in as a local file. All browsers will treat the applet as untrusted code and throw security exceptions to protect your computer. There are too ways round this; Firstly you could sign the applet, to be honest this is a waste of time because 90% of people don't know how to sign applets and there is no real gain compared with the other method. When choosing to write your Java program as an applet as opposed to an application you are presumably going to be using it for a web based purpose. To test them you need a http server. You could write one, it isn't that hard but you are better getting a copy of Apache because it has been tested and is secure.

Once Apache is installed your browser will view it as a web server and you can access the html pages by entering your computer name or your IP address. If your machine is standalone or netwroked you can obtain the IP address by entering MS-Dos and typing IPCONFIG.

Once you have apache installed the applet can be accessed from the Apache's directories and any references to files in those directories (or even requests for sockets) will be accepted. You can access any file from the computer/IP address the applet was called from. You don't have to worry about somebody accessing your files unless they are able to put an applet on your machine.

Chris.