• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Accesing image files in java beans

 
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the actual file being stored?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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:
 
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!