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

Accesing image files in java beans  RSS feed

 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
I am developing an application which needs to access images files put in image folder in java bean file.
I tried a lot by using different methods and failed always, except in case of absolute path which I dont want to use.

My directory structure is as below

/Web-apps
/MyApp
index.html
myjsp.jsp
/Images
img1.png
/WEB-INF
/classes
/beans
GraphBean.java

In GraphBean.java I have the code


I want to use img1.png file in place of image file name.
I want to know how this can be achieved without using absolute path.

Thanks in advance
Aniket
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the actual file being stored?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its
/Images
img1.png


But that would need an absolute path in the bean right.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Ghorpade:
I think its


But that would need an absolute path in the bean right.


Yes, but you can use the ServletContext to generate the absolute path and pass it to the bean. For example:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that getRealPath() will not work if you run the web app out of an unexpanded war file (I never do that, but some do). If you want to account for that eventuality, explore the getResourceAsStream() method.
 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!