• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

storing and viewing image files

 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
My application needs to store and view images on the fly. Where should I store the image and be able to get its http path in context?
1. storing in WebContent of the application. But i have a hard time getting its real path in the server's file system. Is this feasible?
2. storing in a global context(eg.the application_server_root), but the files stored on there can't be accessed by web client. Is there a place under application_server_root that is accessible by the web client?
i am deeply confused.
please help!
thanks!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most portable way would be to define a directory outside of your app.
Configure it from either a context-init-param or a servlet-init-param and then write a servlet that streams the images from that directory.

This will work whether your app is being run as a packed war file or as an exploded file system.

If you know your app is always going to be run from an app server that can and will run it from an exploded file system (like Tomcat) then you can use ServletContext.getRealPath("/") to find the root of your webapp.

If you're planning on running in a clustered environment, you may want to consider storing the pics in a database or creating a separate and central image server.


You can check out SimpleStream at http://simple.souther.us for an example of streaming images from a servlet. This example, however, uses ServletContext.getResourceAsStream which is appropriate for resources stored within your webapp. To access a file outside of your webapp, you will need to get the input stream with a FileReader. This is a small change and should be pretty simple.
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
thanks for the explanation.

The most portable way would be to define a directory outside of your app.
Configure it from either a context-init-param or a servlet-init-param and then write a servlet that streams the images from that directory.
[/qoute]
Is it possible to define a directory outside of my app and then access&create hyperlinks for the images from that directory without going through a servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to define a directory outside of my app and then access&create hyperlinks for the images from that directory without going through a servlet?


No. Not unless the files are within the realm of another app.

I have seen sites that set up Apache just to serve such files, but I think Ben's servlet route is simplest and best.
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked out the simple StreamServlet, it's good for serving image files. I pass the servlet an image file name, then the image is served in a browser page. But if I want to imbedded the image in a page that has other text in it, can this be done? Of course it's easy if the images are stored under WebContent, but in my case they are possibly stored outside the WebContent, that's why I needed a Streaming servlet in the first place.
Can this be done? How? I also checked out cewolf, it's good for imbedded charts filled with data, but not for imbedded images drawn from image files.
Any idea?
thanks!!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A webpage with images is just a series of requests. Each image gets it's own request. The browser puts them all together.
The index.html page in simpleStream is a perfect example. It has text (from the html page) and 3 images that were all streamed from the same servlet.
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally understand now, thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic