> -----Original Message----- > From: Patrick Martz [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, December 17, 2002 1:42 PM > To: '[EMAIL PROTECTED]' > Subject: File access from a servlet. > > > Hi all. > > I'm currently working on a java servlet with tomcat and I > want it to be able to load a different data file dependent on > certain parameters passed to the servlet. The problem is that > if I just try to open the file with the file name (i.e. > FileInputStream fin = new FileInputStream("blah.dta");) it > fails to find the file. I am guessing this is because the > runtime directory is different from the directory the servlet > is running in? (the data file and the servlet are in the same > directory, but the servlet fails to find the file still). So > my question is, is there a way to get the current runtime > directory for Tomcat so that I can perhaps supply a relative > path to get to the file and have the servlet be able to open > it? Thanks! > > Patrick > > P.S. For debugging purposes I HAVE tested opening of the file > from a stub class and it works just fine that way, but fails > from the servlet. >
Rather than using FileInputStream, try ServletContext.getResourceAsStream. This is the preferred method for accessing files within your webapp. You pass in the path relative to the context root directory. An added bonus is that this will still work if you deploy your webapp as a WAR. http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletConte xt.html#getResourceAsStream(java.lang.String) -- Tim Moore / Blackboard Inc. / Software Engineer 1899 L Street, NW / 5th Floor / Washington, DC 20036 Phone 202-463-4860 ext. 258 / Fax 202-463-4863 -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
