Question

I am trying to access a resource file from a servlet but getting HTTP Error 500 - access denied:

File file = new File("//warChildFolder//myFile.txt");
InputStream is = new FileInputStream(file); // <--error on this line

I am on google-app-engine.

Any help appreciated!

Was it helpful?

Solution

Google App Engine docs talk about "white listing" a file. Is that in play here?

I also wonder about this:

File file = new File("//warChildFolder//myFile.txt");

Doesn't the leading slash make this an absolute path?

I'd try it like this:

File file = new File("WEB-INF/warChildFolder/myFile.txt");

Make the path relative to the WAR root and be explicit about WEB-INF.

OTHER TIPS

I'm not sure about Google App Engine but in my experience the only solution that works across containers and platforms is to use ServletContext.getRealPath().

new File(servletContext.getRealPath("/WEB-INF/warChildFolder/myFile.txt"));

The spec says: use forward slashes and a leading slash. This gives you platform independence and you're not relying on the process' current directory.

Does it work if you use single path separators?

(updated to use relative paths):

File file = new File("warChildFolder/myFile.txt");

You need to escape the "\" character in Strings, so use "\", but a single "/" is all that is needed.

Update: It may be that the path being processed is not the same as you expect, you can try logging the absolute path of the file (with file.getAbsolutePath()) to check this.

Another thing to check is that the process has read permissions on the folder/file. If you're not on Windows this can be a problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top