2

I use java, tomcat, jsf and primefaces

and I have a field "image" in my product table and I 'd like to ask you which is better : save the image in the database or in one directory in the server

if the second case is the best, please explain me more how to manage it (I never had the opportunity to meet this case)

thank you in advance

atbegin-but
  • 263
  • 1
  • 8
  • 19
  • How big are your images? [**To BLOB or Not To BLOB**](http://research.microsoft.com/apps/pubs/default.aspx?id=64525) discusses some of the factors that you should consider when deciding whether to store images in the the database or on the file system.... even if it is written for a different stack than what you're using. – Michael Fredrickson Mar 25 '13 at 23:26
  • 1
    Look [here](http://stackoverflow.com/q/3748/1530938) for direction – kolossus Mar 26 '13 at 01:11
  • in my humble opinion having image in database overloads database and also server. I use a third-party server directory to store images (Amazon S3) which has good performance and is very cheap. – damian Mar 26 '13 at 18:48
  • @atbegin-but Please follow my answer on this post. http://stackoverflow.com/questions/16342537/primefaces-pfileupload-listener-method-is-never-invoked-for-mode-simple/29293844#29293844 – Umair Mar 27 '15 at 05:37

1 Answers1

0

Whether or not you want to save the image binary in database or save the image file in server directory depends on your need.

The never ending debate is still going on and you can find it easily with several key strokes.

Here is how I do it(second approach) and it never disappoints me:
Assuming you are building an e-commerce website and want to host seller uploaded images such as product images and show them later.

  1. Save the uploaded image files physically on your server outside of your war .Let's say the directory is /baseFolder/ and the full path of the file is /baseFolder/path/to/image.jpg.
  2. Add a virtual host in Tomcat so that your http://localhost:8080/imageBase/ is pointing to the physical directory /baseFolder/. There are many other ways to make this kinds of mappings.
  3. Store the relative path /path/to/image.jpg in your database. So that when you want to show it you can simply say:<img src="#{webappRoot}/baseFolder/#{pathRetrievedFromDatabase}"/>. In this case it would be <img src="#{webappRoot}/baseFolder/path/to/image.jpg"/>

There are of course a lot of different ways to achieve the same thing. This is the simplest way I can think of to explain how it is managed.
Hope it helps.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39