• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to display img (.jpg) located on hard drive?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to store image (.jpg) files in a database and pull out image to display on jsp webpage. Browsing web suggest that storing images in a database (MS Access) is not ideal and it is better to have images in a folder and then have links to them in the database - sounds fine to me so wondering if anyone has any comments about the two approaches?

Anyway, I have fallen at the first hurdle of trying to display image given its full path on the hard disc - the image is not part of my web app i.e. not in the war file. In my JSP I am using <img> tag below which does not display the image. (I have tried variants of /, \\ and / as file separators to no avail).



A quick read about this indicates that the web application, for security, cannot access files outside its deployed folders which ties in with what I see. (I do not see the red cross within a white box but a small jpg icon, clicking on it for properties gives: name, size and Not Available for other properties)

Is there a way around this?

I supposed I can have my action class read the image file from the hard disc then copy to a folder in my deployed app but that seems cumbersome.

 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The URL for any web content, including images, must be relative to the root directory of your web application. Allowing URLs with general disk access would be a major security leak (anybody could read any files on your system). The only way around this is to write a servlet that services images - it would get an image request and then look that image up from elsewhere and supply the image to the web page.
 
m pa
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for that clear answer. Servlet is one way to do it but it appears that JBoss (I am using 4.2.3GA) can be configured to access external directories.
see: https://community.jboss.org/wiki/ExternalDirectories

Now the example for JBoss 4.2 states:
Serving Static External Files in recent 4.x versions of jboss

This works for Jboss 4.2.x and probably other versions as well.

Step 1 (optional): - Copy a default web.xml from <JBOSS_HOME>/server/<config-name>/deploy/http-invoker.sar/invoker.war/WEB-INF and place it in your external directory, for example: /home/pgib/images/WEB-INF

Step 2: Edit <JBOSS_HOME>/server/<config-name>/deploy/jboss-web.deployer/server.xml and add a Context element under Host.(Step similar to standalone TC)

<Host name="localhost" ...>
<!-- ADD static images DIRECTORY -->
<Context path="/images"
docBase="/home/pgib/images"
reloadable="true">
</Context>
<!-- The rest of your Host entity -->
</Host>

Step 3: Enjoy. This will enable Tomcat to serve up static content (like html, images etc) from a directory /home/pgib/images and the url will be "http://localhost:8080/images"


The docBase path example above is linux/unix but it can be Windows path, such as:
C:\Users\mpxxxxi\Documents\Work\JDBC\images

--------------------------------------------------------------------------------------------------------------------------------------------------------------

A super example on how to implement a servlet for Struts 2 is here:
http://www.mkyong.com/struts2/struts-2-dynamic-image-example/


 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic