• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File access from an applet

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get someting straight in my mind. I'm doing
a Postgraduate Java course at the moment and one of the
assignments seems to require an applet to read a file.
The assignment specifies that a previously developed class is to be used to hold some data in an applet, and that class has a single constructor with a single String parameter which gives the name of a file. Inside the constructor, the specified filename is used to create a FileInputStream from which the data is read. This worked fine in a locally running application.
My "tutor" assures me that it is possible for an applet to use this code to open a file on the server from an applet but that seems so unlike all my Java experience that I'm boggled.
What do you think? can it be done - can I use a FileInputStream to read from a remote file? or is my tutor missing the point of the assignment?
Thanks,
------------------
Frank Carver
[ Personal: frank@efsol.com http://www.io.com/~efficacy ]
[ At Work: frank.carver@bt.com tel +44 (0)1473 227371 ]
 
Michael Finney
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have a second to reply to this:
FileApplet
Does that help?
Thanks,
Michael
[This message has been edited by Michael Finney (edited March 24, 1999).]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. I already knew that I can get a stream from a URL. In fact, I use that elsewhere in the project to load a remote properties file. The problem I have here is that the existing API is defined to take a single String parameter, specifying a filename, which is used to construct a FileInputStream.
I just can't see how this would work (and I can't seem to make it work, either...)

------------------
Frank Carver
[ Personal: frank@efsol.com http://www.io.com/~efficacy ]
[ At Work: frank.carver@bt.com tel +44 (0)1473 227371 ]
 
Jim McCabe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using FileInputStream, which seems to be limited to files, create a URL out of your filename and then call URL.openStream(). It returns an InputStream object which you should be able to use just like your FileInputStream was used.
This will work with local files as well as files over the network.
For example, in your applet, if you want to access some remote file that lives in the same directory as your applet's HTML file, do something like this:
URL url = new URL(applet.getDocumentBase(), "myfile.txt");
InputStream stream = url.openStream();
// Do whatever you want to do here...
stream.close();
This should work locally when you're testing your applet on the local filesystem, as well as remotely when you're running the applet over the net.
-- Jim
jmccabe@pobox.com
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that was what I ended up with.
After a lot of testing, what I have found out is that (a) my tutor is a bozo and (b) there is one case in which you can usefully pass a filename into code like this.
The existing code (which apparently may not be changed):
<pre>
public AccountDetails(String s)
{
properties.load(new FileInputStream(s));
}
</pre>
The recommended solution by the "tutor":
<pre>
URL url = new URL(getDocumentBase(),"accounts.prp");
AccountDetails ac = new AccountDetails(url.getFile());
</pre>
This is plainly gibberish, but does just happen to work if run in appletviewer (rather than a real browser) with the name of the associated html file (with no path or network information) as a parameter, eg:
appletviewer accounts.html
Obviously this "tutor" has never bothered to try serving his code from a real server, or running it in a real browser. Hence (a).
If I get marked down by this idiot I'm going to kick up a lot with the university authorities. It's just not good enough.
A frustrated Frank.
 
Eric Barnhill
Rancher
Posts: 233
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope you don't mind me dredging up an old thread, but it contains something that almost answers my problem, but not quite, and I'm hoping someone can clear this up for me.
Here is the putative answer:

Instead of using FileInputStream, which seems to be limited to files, create a URL out of your filename and then call URL.openStream(). It returns an InputStream object which you should be able to use just like your FileInputStream was used.
This will work with local files as well as files over the network.
For example, in your applet, if you want to access some remote file that lives in the same directory as your applet's HTML file, do something like this:
URL url = new URL(applet.getDocumentBase(), "myfile.txt");
InputStream stream = url.openStream();
// Do whatever you want to do here...
stream.close();
This should work locally when you're testing your applet on the local filesystem, as well as remotely when you're running the applet over the net.

Okay, this doesn't work for me. When I create a similar URL using an absolute address "http://(content omitted)/comments/commentfile.txt" I get an internal server error, just like I would if I typed the location in my browser. My InputStream picks up the http code for the internal server error and prints it. This file does exist in this folder and I can read it using Unix. So, is the above answer right? If so, how do I get to my file? Does it rest on using a relative pathname like he did?
Thanks
Eric
[This message has been edited by Eric Barnhill (edited June 21, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic