views:

31

answers:

1

This question is related to "Java Web Application - File Upload".
Is it possible for a Web Application to read a file from the host file system?
My guess is that for security reasons the answer is no - but I guess it should be possible for it to read files in directories below the root of the application.
If so, could someone supply an example code snippet.
Thanks

+1  A: 

I'm using the Apache Wicket web-framework, deployed on the Glassfish 2 web-app container, and this snippet works fine for me:

//Read file content
FileInputStream in = new FileInputStream(new File("absolute path"));
byte[] bt = new byte[(int) file.length()];
in.read(bt);
in.close();
return bt;

Could however be related to the way the web application server is setup, so your mileage may vary..

Tim
@Tim Hi. Is "absolute path" below WEB-INF? Or is it a random location of the file system. Did you have to configure special permissions in the security manager?
leftbrainlogic
It's a random location on the file system.. I've not touched the security manager, so I'm working with whatever the Glassfish instance gave me. I'm not the one administrating this server however, so I can't say what security restrictions are in place (but I have heard rumors saying it's run as root.. =\)
Tim