views:

4164

answers:

5

I am developing a web application using Struts 2.1.2 and Hibernate 3.2.6.GA. I have an entity, User, which I have mapped to a table USERS in the DB using Hibernate. I want to have an image associated with this entity, which I plan to store as a BLOB in the DB. I also want to display the image on a webpage along with other attributes of the User.

The solution I could think of was to have a table IMAGES(ID, IMAGE) where IMAGE is a BLOB column. USERS will have an FK column called IMAGEID, which points to the IMAGES table. I will then map a property on User entity, called 'imageId' mapped to this IMAGEID as a Long. When rendering the page with a JSP, I would add images as <img src="images.action?id=1"/> etc, and have an Action which reads the image and streams the content to the browser, with the headers set to cache the image for a long time.

Will this work? Is there a better approach for rendering images stored in a DB? Is storing such images in the DB the right approach in the first place?

A: 

If you want to display the user image directly with their properties perhaps you can consider embedding the image data directly in the HTML.

Using a special data: URL scheme you are able to embed any mime data inside a HTML page, the format is as follows:

data:<mimetype>;base64,<data>

needs to be replaced by the mime-type of your data (image/png for instance) and is the base64 encoded string of the actual bytes of the file.

See RFC 2557.

Example:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAAmCAYAAAB52u3eAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH1AkICzQgAc6C2QAACrxJREFUeNrtmnl0VdUVxn/3vnnIPBKmJECYkoAEERArgxGqCDIUkKVoqRZKUVREnC1SXXWJlKo4VrEi4EAUEARUtM5oVcAwZhZIyEySlzfe4fSPlzwSEzGUuLpa2Gu9te49Z99zzvvu3vt8e58rCSEAUBWNoyV1wZtzVHokR0pGkwEASQhB2bF6sX/vCZxhFjzuwDkJSlWFm4LDVYyf1I9ho3pKRiWg8f23ZfTPTCQ+0XkuGwzPr/qCZ1Z8zsDBXTCWFNYKq81EVIwNm910TgNzwbBufPlxCR9uzxMyQEO9j/MSlLpaLwDyeSjal3MOmBunb/gvABNQoepku12qqrVpUxTtjKdQVY1mivFTY7Wn09x/rKSuQ/MYOwsTUViKOn0RxifvRYqLAuDw/goeuO1dIiJtVJa7+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ft7feuSc/jgxe2JfqTk7/zeNpiqnFESz8wAAAABJRU5ErkJggg==">
smink
I just read that this does not work with IE, until version 8 is released. Also, the image is not cached on the browser. So, despite the ease of coding, I think I will use my original scheme.
binil
A: 

Internet Explorer does not support that style of image embedding.

Tom R
Just tested http://rifers.org/blogs/gbevin/2005/4/11/embedding_images_inside_html with IE7 and it works just fine.
smink
The image in Geert's post does not show up on IE6/WinXP.
binil
+4  A: 

Yes your suggested solution will work. Given that you are working in a Java environment storing the images in the database is the best way to go. If you are running in a single server environment with an application server that will let you deploy in an exploded format technically you could store the images on disk but that wouldn't be the best practice. One suggestion would be to use a servlet instead of a JSP. To get good browser behavior you want the browser to think that the file type that it is displaying matches the file type that it is expecting. Despite the existence of mime type headers the file extension is still really important. So you want a link that looks like this:

<a href="foo.jsp"><img src="imageservlet/123456789.png"></a>

Where 123456789 is the primary key of your image in the database. Your servlet mapping would look like this:

<servlet>
  <servlet-name>ImageServlet</servlet-name>
  <servlet-class>com.example.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>ImageServlet</servlet-name>
  <url-pattern>/imageservlet/*</url-pattern>
</servlet-mapping>

Then in your servlet simply parse the request URL for the image ID rather than using the query string as the query string will confuse some browsers. Using the query string won't break browsers outright but you'll get odd behavior with regards to caching and some browsers may report the content as unsafe.

Craig Wohlfeil
I have implemented this using this mechanism (using an ServletResponseAware Action instead of a Servlet). I am not picking this as an accepted answer hoping that other mechanisms might come up. I have upvoted this, so that this is the best known answer as of now.
binil
A: 

Your suggested solution would work perfectly. I have done the same thing.

But you don't need a servlet for this. Struts2 has already a stream result.

See this Struts 2 Example which describes exactly what you want.

kazanaki
A: 

kazanaki any chance of braking down what to do with the stream? I'm a little confused

Kris