File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes storing and viewing image files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "storing and viewing image files" Watch "storing and viewing image files" New topic
Author

storing and viewing image files

jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
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

Joined: Jan 10, 2002
Posts: 61103
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Apr 07, 2004
Posts: 139
I totally understand now, thanks!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: storing and viewing image files