SSaaM
SSaaM

Reputation: 108

Reading a file on a server

I have an applet which is partly designed to (only) read in text files and make stuff based on that. in my applet, this is what I have as the "read" method which reads in files:

public void read (String file1) throws IOException
{
    str.removeAllElements (); // str is a global vector
    BufferedReader dia = new BufferedReader (new FileReader (file1));

    for (;;)
    {
        strc = dia.readLine ();
        if (strc == null)
            break;
        str.add (strc);
    }
}

this works fine when I'm running it through the JVM, but when I take it online the files that I want to access are not accessible even though they are hosted on the same server and folder.

HTML for my applet looks like this:

<applet
codebase = "[the url that hosts my class and text files]"
code = "[my class file].class"
width = ###
height = ###>
</applet>

The specific error I'm getting is:

AccessControlException
access denied ("java.io.FilePermission" "dial1.txt" "read")

So if anyone could help, that would be awesome!

Upvotes: 0

Views: 1263

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168815

I suspect the real problem here is using a java.io.File. Put server/client aside for the moment and a sand-boxed applet cannot establish a File - at all. But to extend that slightly, a trusted applet can establish a File - but only one that points to files on the local drives of the client machine. A File can never point back to the server, they just don't work that way.

So that leads to. The proper way for an applet to access resources is by URL. Java uses URLs a great deal, even for accessing classes in Jars.

A sand-boxed applet can establish an URL pointing back to the server from which it was deployed.

As to how to form that URL. The URL can be formed relative to the code base (the location of the Jars/classes) or document base (the location of the HTML).

Upvotes: 1

Korbi
Korbi

Reputation: 1448

Applets run inside the web browser. Hence, on the computer of the user who downloads your applet. So even if the files you are looking for exist on the computer of the user you won't be able to read them because you don't have file system access to people surfing the internet. Read your local file on server-side. So in your case, you probably need a servlet instead of an applet.

Upvotes: 0

Related Questions