1

I have a .war file. And I need access to file located inside ROOT_OF_WAR/someFolder/myfile.txt

How I can access to it using ServletContextListener?

Does ContextClassLoader#getResourceAsStream(resourceName) point to root of web-app (i.e. root of.war file)?

Or I need to access it using ServletContextEvent#getServletContext().getResourceAsStream("someFolder/myfile.txt"); ?

WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • AFAIK, you need the latter one - why don't you just try it? – home Sep 21 '12 at 14:48
  • This link http://stackoverflow.com/questions/12883861/how-to-access-a-file-under-web-inf-folder-in-java-class has a lot of good information. – Suketu Bhuta Jan 09 '15 at 19:44

1 Answers1

4
ServletContext context = servletContextEvent.getServletContext();    
File file = new File(context.getRealPath("/someFolder/myFile.txt"));

That is all you need. See here.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143