aspose file tools*
The moose likes JBoss/WildFly and the fly likes how to display img (.jpg) located on hard drive? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "how to display img (.jpg) located on hard drive?" Watch "how to display img (.jpg) located on hard drive?" New topic
Author

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

m pa
Greenhorn

Joined: Apr 17, 2013
Posts: 17
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.

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5830
    
    7

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.


JBoss In Action
m pa
Greenhorn

Joined: Apr 17, 2013
Posts: 17
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/


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to display img (.jpg) located on hard drive?