Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

InputStream reference  RSS feed

 
prasad agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,...

In a J2ee web application, how do i get an InputStream reference to a file in say like WebContent/configfiles/file1.config

This is not working:



I mean i am able to get a reference if the file is in WEB-INF by using the following code..



I appreciate if you guys can throw some light
Thanks a lot
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file system and context path have nothing to do with each other. So your call to file system functions using the context path will fail because the context path is not part of the file system.

If you are going to use file system methods, you need to map the web app to its physical file system location using ServletContext.getRealPath(). Note that this method has limitations.

However, the most appropriate means for accessing files within the web app (at least in my opinion) is ServletContext.getResourceAsStream().

Note that this is not the same method as your classloader example in you post.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getContextPath() is going to get you the name of your application, not a path on the file system.

Try again using an absolute path.
 
prasad agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear Bibeault and Ben Souther,

Thanks a lot )

This line of code has done the work:

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to keep in mind is that getRealPath isn't always portable.

If your application was deployed as an exploded file system or if your container unpacks it before deploying, getRealPath will work. If on the other hand, your app was deployed straight from a war file, getRealPath will return null (as there is no file system path that can be returned).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!