How to upload Dynamic Images to JSF?

Asked

Viewed 3,389 times

6

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

I don’t want to use the field blob in the database, then how do I resolve the problem of the images storage folder, since the file . War is closed.

  • You know about where to write or how to use JSF to upload?

1 answer

4

Upload approaches

There are two commonly used upload file saving approaches: saving to DB or to a folder off-project

Error starting the project

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 made, in the case of Tomcat, it would be inside the webapps folder.

'Cause that’s bad practice?

Who takes care of the folders created inside a server, is himself. It may well delete, rewrite, copy or do whatever you like with the files found there.

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

Saving files in a database

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

The disadvantages would be that:

  1. It would weigh the database. When returning an image of the database this could weigh the network and the traffic of the data.
  2. A query by the user could return the photo (even if not needed) which could slow down the process (in case of heavy images).
  3. In case you just wanted to view the photo you will need through the system. There is no facility to make 2 clicks and view the photo (unless DB does it for you). Newer database has the option to save the photo in 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 this kind of functionality.

Saving files to a folder

The advantages of saving files to a folder would be:

  1. Ease to view the files. A double click would suffice that you could open/edit the file without disturbing the system.
  2. It does not weigh the network. In this case you would use the IO system of the operating system, which would leave the network free to communicate with the DB.
  3. Facilitates the model in the database. You won’t need to map a blob or whatever to the database. Just a String pointing 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 I know of, others may already have the function of searching outside the server).

There are solutions you could make to display the images:

  • Use a Servlet. That’s right! You could have a Rvlet that would read the images on a given path and display back to whoever was looking to see it. All you had to do was return a URL like: uaihebert.com/imagem33.png that Servlet would display that image. An example of Preview returning image has here: http://www.javatpoint.com/example-to-display-image-using-servlet
  • Another thing that could be done, in this case using Primefaces, would be via code to load the bytes of the image in a certain format and then return within an object expected by the first faces. (Now their website is down, I can’t get an example there)
  • Very enlightening your reply! Thank you. Remember the name of the component primefaces? The images would be dynamic..

  • It would be the Dynamic image (I think that’s how you write).

  • Thanks! was of great help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.