How to upload Dynamic Images in JSF?

6

What is the best strategy for uploading and using images in JSF?

I do not want to use blob field in the database, so how do I solve the image storage folder problem, since the .war file is closed.

    
asked by anonymous 29.01.2014 / 03:56

1 answer

4

Upload approaches

There are two approaches to saving commonly used upload files: saving to DB or to a out of project folder

Project start error

It is very common to see, who is starting to work with java web projects, the upload being saved in the same project directory. In other words it is common for people to save the files inside the same folder where the deploy was performed, in Tomcat's case it would be inside the webapps folder.

Why is this a bad practice?

Who takes care of the folders created inside a server, is himself. It can very well erase, rewrite, copy or do as you please with the files found there.

In this case the correct would be to have a folder outside the deploy area of the server for example. Something like: "C: \ uploads"

Saving files to a database

The advantage of this approach is that when backing up the database, the image is automatically backed up. Another advantage is the fact that the code is simpler, to search for an image just a simple query.

The disadvantages would be that:

  • Weigh the database. When returning a database image that could weigh the network and data traffic.
  • A query by the user could return the photo (even if it does not require it) which could slow down the process (in the case of heavy images).
  • If you just wanted to view the photo you will need it through the system. There is not the facility to give 2 clicks and to visualize the photo (unless the DB does it for you). Newer database has the option to save the photo to a folder but as if it were part of a DB column (I know SQLServer does this); the problem is that many companies use free banks that do not have that kind of functionality.
  • Saving files to a folder

    The advantages of saving files to a folder would be:

  • Easy to view files. It would just double click that you could open / edit the file without disturbing the system.
  • Do not weigh the net. In that case you would use the operating system's IO system, which would leave the network free to communicate with the DB.
  • Facilitates the model in the database. You will not need to map a blob or whatever it is for the database. Just a String pointing to the Path of the file.
  • So how do I display the photos?

    In the case of JSF you will run into the problem that the components, natively, only display images that are inside the server (at least those that I know, others may already have the look-out function).

    There are solutions you could do to display the images:

    • Use a servlet. Exact! You could have a servlet that would read the images on a particular path and display back to whoever you want to see. All you had to do was return a URL like: uaihebert.com/image33.png that the servlet would display this image. An example servlet returning image has here: link
    • Another thing that could be done, in this case using the Primefaces, would be via code to load the bytes of the image into a certain folder and then return inside an object expected by the primefaces. (Now their site is down, I can not get an example there.)
    29.01.2014 / 04:39