java - Show image on jsp with spring mvc and hibernate -


I have a blob type image in mysql database and I want to show the image on jsp. I'm using Hibernate and Spring MVC. This is my model class:

  @Repository @Entity @Table (name = "photo") public class image {@ManyToOne (bring = FetchType.LAZY) @JoinColumn (name = "fk_id_users", drain = false) Private user user; @ Id @ column (name = "id_foto") @ Generated Values ​​(strategy = generation type AOTO) Private id ID_photo; @column (name = "tip") private string tip; @column (name = "size") private string size; @column (name = "noam") private string filename; @column (name = "image") private byte [] image; // getters and setters  

This is my controller:

  @Controller @SessionAttributes ("UserSession") public class LoginController {@Autowired Private UsersService usersService; @RequestMapping (Value = "loginUsers", method = RequestMethod.POST) Public ModelAndView loginUsers (HttpServletRequest Request, @ RequestParam ("username") string user name, @RequestParam ("password") string password) {User user = usersService. LoginUsers (username password); If (user == tap) {ModelAndView MV = New ModelAndView ("Login"); MV.addObject ("Errol Lolin", "Username / Password Alert"); Return MV; } And if (user.getAmministratore () == incorrect) {request.getSession (). SetAttribute ("UserSession", user); ModelAndView mav = New model and visual ("HomeUtente"); Mav.addObject ("Galleria", usersService.getAllFoto ()); Return mawa; } Other {request.getSession (). SetAttribute ("UserSession", user); ModelAndView mav = New model and view ("use"); Mav.addObject ("lista", usersService.getAllUtenti ()); Return mawa; }} @RequestMapping (value = "logout", method = RequestMethod.GET) Public ModelAndView Logout (HttpServletRequest Request) {request.getSession () invalid () .; // In the Sessional return, the new model and view ("Login") is invalid.}}  

And in my jsp I used it to show my image in the image list because each user displayed There is a gallery to:

  & lt; Img alt = "Kangoo_image" src = "data: image / jpeg; base64, $ {galleria.image}" />  

When I'm trying to display it in my JSP. This is showing some binary like [B07 07373] How can the image be shown in JSP?

To add it to the file system without storing on the JSAP, you need to base 64 encoding of the byte array Have to do. The following lines are easily done by

  byte [] encodeBase64 = Base64.encodeBase64 (usersService.getAllFoto ()); String base 64 encoded = new string (symbolic brace 64, "UTF-8"); Mav.addObject ("Galleria", usersService.getAllFoto ()); Both are from IOUtils and Base64 org.apache.commonsEndFragment 

.

Comments