aspose file tools*
The moose likes Servlets and the fly likes I cannot access ordinary file from servlets by giving relative Path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "I cannot access ordinary file from servlets by giving relative Path " Watch "I cannot access ordinary file from servlets by giving relative Path " New topic
Author

I cannot access ordinary file from servlets by giving relative Path

Pavitdeep Singh
Greenhorn

Joined: Nov 28, 2002
Posts: 1
Actually i want to access .txt file from servlet.
When i give the absoulte path it takes the file .otherwise it's giving the error . FilenotFound Exception . I am using Jrun web server.If anybody knows the answer please tell me immediately as i am executing some important project
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Relative path makes an assumption about the "current directory" - in a servlet you have no control over the current directory.
Seems to me your choices are:
1. Use servlet init parameters to specify a base path for your working files.
2. Use the ServletContext methods for getting the real path from a application-relative path.
Incidently - "please tell me immediately" doesn't do the slightest good around here - people either answer or they don't.
Bill
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I use this code on several websites (Tomcat & Resin driven) for reading content for the pages. I think you will get the idea from it
URL url = getServletContext().getResource("/siteText/"+pageName+".txt");
InputStream is = url.openStream();
if (is != null){
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s;
while ((s = br.readLine()) != null){
sb.append(s);
sb.append("\n");
}
br.close();
is.close();
}
Hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I cannot access ordinary file from servlets by giving relative Path