• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display image from non-web directory  RSS feed

 
todd runstein
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to display a jpg or gif that is NOT under the root of my web application. For example, an application in tomcat is running in /opt/tomcat/webapps/myapplication. An image resides at /home/username/myicon.jpg.

How would you display this image on a webpage without moving the image into "myapplication"?

Thanks in advance
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't. If the image is used in your web app, then it is part of your web app and should be kept with the rest of the web app's content.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to use a servlet to handle the image request from the browser since Tomcat can't serve the image directly. This is done by having the src attribute in the HTML use a URL for the image serving servlet plus whatever it takes to identify the image. Most servlet books and tutorials have examples.
Bill
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
todd,

What you can do are:
a. read that image as a binary stream
The method you probably use is application.getResourceAsStream()

b. write that binary stream back to browser
The methods you probably use are response.setContentType("image/jpg")
response.getOutputStream()

Dan
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Paul, but if you really want to do this, there is an example app on http://simple.souther.us called SimpleStream.
In it there is a servlet that streams images from under the WEB-INF directory.

It gets the input stream by using ServletContext.getResourceAsStream.
You could just as easily use a FileStream if you wanted to stream images from outside of the app.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!