Archived

This topic is now archived and is closed to further replies.

[java] applet read file on server

This topic is 5913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I''m getting this error in my applet: Exception: java.security.AccessControlExeption: access denied (java.io.FilePermission ..\maps read) I''ve tried putting in the full path to the directory and it still doesn''t work. Also, I put in a public static void main(String args[]) that uses the readDir method and it worked perfectly so I don''t think it has anything to do with the actual file system but rather the applet itself. I thought maybe the applet was trying to access a file on the users computer but I''m not sure how to enforce it to read off the server. any ideas? Thanks for the help!

Share on other sites
to read a file from the server, you should use an url to do
that:

URL serverUrl = new URL("http://www.myserver.com/readme.txt");

then you can use serverUrl.openStream() to get an InputStream
and read from that.

keep in mind, that an applet is only allowed to access the server it was
loaded from, all other servers are forbidden to talk to due
to applets security mechanism.

Share on other sites
ahh. Thanks alot!

edit:

Couple questions..
Why can't I use normal file io to read something from the server? Isn't the applet running on the server?

If it must be this way, then do I need to create a socket to do this? Right now I have this:

          /*        Read text file and return the        string that contains the data.        */                 public String readFile(String fileName)throws IOException {                URL serverUrl = new URL(fileName);                InputStream i = serverUrl.openStream();                StringBuffer buffer = new StringBuffer();                int k;                while((k = i.read()) != -1){                        buffer.append((char)k);                }                i.close();                return buffer.toString();        }

and I'm getting this error:

Exception: java.security.AccessControlException: access denied (java.net.socketPermission www.mysite.com resolve)

thanks for the help

[edited by - mr2071 on June 4, 2002 4:12:58 PM]

Share on other sites
quote:
Original post by mr2071 Why can''t I use normal file io to read something from the server? Isn''t the applet running on the server?

No, the applet is running on the user''s machine and has to connect to the server through the internet. You can only read from URLs on the server''s website, not from the server itself. So if you can see your text file at http://www.mysite.com/text_files/my_cool_file.txt, then the applet should be able to read from it.

quote:

If it must be this way, then do I need to create a socket to do this? Right now I have this:
...
and I''m getting this error:

Exception: java.security.AccessControlException: access denied (java.net.socketPermission www.mysite.com resolve)

Are you running the applet from the web server it''s connecting to? You can only connect to the server the applet was downloaded from.

Share on other sites
quote:

No, the applet is running on the user''s machine and has to connect to the server through the internet. You can only read from URLs on the server''s website, not from the server itself. So if you can see your text file at http://www.mysite.com/text_files/my_cool_file.txt, then the applet should be able to read from it.

I can see it the text file at the location but I still get the error.

quote:

Are you running the applet from the web server it''s connecting to? You can only connect to the server the applet was downloaded from.

Yes, I am running the applet from the same machine as it is trying to connect to.

Share on other sites
Are you suing win9x? I think I remember their being a problem with client/server apps not connecting if they are on the same machine.

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Share on other sites
win2k pro, ie6, jdk14

Share on other sites
Try "new URL(getDocumentBase(), filename)" instead

Share on other sites
This creates and uses a stream from an URL:

    URL URLMen=null;URLConnection httpServ=null;try        {        URLMen = new URL("http://server.dot/puthere/your/url.txt");	httpServ=URLMen.openConnection();	}catch (Exception e)	{	// Error	}			httpServ.setDefaultUseCaches(false);httpServ.setUseCaches(false);		// Get the file		String Line;try	{	BufferedReader Input=new BufferedReader (new InputStreamReader (httpServ.getInputStream()));	Line=Input.readLine(); // Reads lines	Line=Input.readLine();	Input.close();	}catch (IOException e)	{        // Error	}

As you see I'm not doing anything with the lines readed, though.

Real programers are not afraid from maths! (I am)
(from an Asfixia Member I think)
JJpRiVaTe (Private Zone)

[edited by - jjmontes on June 4, 2002 6:53:01 PM]

Share on other sites
Thanks for the help in figuring this out. I was able to get it working with this:

        public String readFile(String fileName)throws IOException{                URL serverUrl = new URL(fileName);                URLConnection urlc = serverUrl.openConnection();                BufferedReader br=new BufferedReader (new InputStreamReader (urlc.getInputStream()));                StringBuffer buffer = new StringBuffer();                int k;                while((k = br.read()) != -1)  {                        buffer.append((char)k);                }                br.close();                return buffer.toString();        }

Quick question. If I wanted to read the contents of a directory, would I do it much the same as this? How would you go about separating the filenames? Thanks.

EDIT:
Ok maybe it didn't work. I wrote a quick main to run this function from command prompt and it spit out what was in the file at the url but when I try to run it in the applet, I still get

Exception: java.security.AccessControlException: access denied (java.net.socketPermission www.mysite.com connect, resolve)

EDIT2:
I'm sorry, I think there's some weird stuff goin on here. Don't worry about trying to fix my problems. I'll get it figured out.

[edited by - mr2071 on June 4, 2002 8:02:19 PM]

[edited by - mr2071 on June 4, 2002 8:05:22 PM]

1. 1
2. 2
frob
14
3. 3
4. 4
5. 5
Rutin
12

• 12
• 10
• 57
• 14
• 15
• Forum Statistics

• Total Topics
632113
• Total Posts
3004181

×