Hibernate - How to get image from database?

I somehow managed to save the image in the database, but now I want to get / display the image again. I just don't know how to do this. Basically, my question is if my ImageDao is missing any further method or what the configuration should look like in jsp.

There is my image class:

package de.hdu.pms.model; import java.sql.Blob; import java.util.Date; import java.util.Set; import javax.persistence.*; @Entity @Table(name="tbl_image") public class Image { @Id @GeneratedValue @Column(name="image_id") private Integer id; private String name; private String description; private String filename; @Column(name="content", columnDefinition="mediumblob") @Lob private Blob content; private String contentType; private Date created; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public Blob getContent() { return content; } public void setContent(Blob content) { this.content = content; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } } 

Imagedao

 package de.hdu.pms.dao; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import de.hdu.pms.model.Image; public class ImageDao extends HibernateDaoSupport { public void save(Image image) { HibernateTemplate template = getHibernateTemplate(); template.saveOrUpdate(image); } @SuppressWarnings("unchecked") public List<Image> list() { HibernateTemplate template = getHibernateTemplate(); //evtl ersetzen durch hibernate template @SuppressWarnings("rawtypes") List images=template.loadAll(Image.class); return images; } public Image get(Integer id) { HibernateTemplate template = getHibernateTemplate(); return template.get(Image.class, id); } @Transactional //hibernate public void remove(Integer id) { HibernateTemplate template = getHibernateTemplate(); Image image = template.get(Image.class, id); template.delete(image); } } 

ImageController:

 package de.hdu.pms.ctrl; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.hibernate.Hibernate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import de.hdu.pms.dao.CocktailDao; import de.hdu.pms.dao.ImageDao; import de.hdu.pms.model.Image; @Controller public class ImageController { @Autowired private ImageDao imageDao; public ImageDao getImageDao(){ return imageDao; } public void setImageDao(ImageDao imageDao){ this.imageDao = imageDao; } //aus dem Tutorial @RequestMapping("/EditImage.html") public String index(Map<String, Object> map) { try { map.put("image", new Image()); map.put("imageList", imageDao.list()); }catch(Exception e) { e.printStackTrace(); } return "edit-image"; } //aus dem Tutorial @RequestMapping(value = "/SaveImage.html", method = RequestMethod.POST) public String save( @ModelAttribute("image") Image image, @RequestParam("file") MultipartFile file) { System.out.println("Name:" + image.getName()); System.out.println("Desc:" + image.getDescription()); System.out.println("File:" + file.getName()); System.out.println("ContentType:" + file.getContentType()); try { Blob blob = Hibernate.createBlob(file.getInputStream()); image.setFilename(file.getOriginalFilename()); image.setContent(blob); image.setContentType(file.getContentType()); } catch (IOException e) { e.printStackTrace(); } try { imageDao.save(image); } catch(Exception e) { e.printStackTrace(); } return "redirect:/AlleImages.html"; } @RequestMapping("/download/{imageId}") public String download(@PathVariable("imageId") Integer imageId, HttpServletResponse response) { Image img = imageDao.get(imageId); try { response.setHeader("Content-Disposition", "inline;filename=\"" +img.getFilename()+ "\""); OutputStream out = response.getOutputStream(); response.setContentType(img.getContentType()); IOUtils.copy(img.getContent().getBinaryStream(), out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } @RequestMapping(value="/AlleImages.html", method=RequestMethod.GET) public ModelAndView list(){ ModelAndView mv = new ModelAndView(); mv.addObject("title", "Images"); mv.addObject("message", "Alle gespeicherten Images"); mv.addObject("image", imageDao.list()); // fΓΌr die jsp bedingung mv.addObject("edit",true); mv.setViewName("list-image"); return mv; } } 

And finally, the code snippet of my jsp, where I wanted to "access" the image, but I failed:

  <h3>Image List</h3> <c:if test="${!empty imageList}"> <table class="data"> <tr> <th>Name</th> <th>Description</th> <th>&nbsp;</th> </tr> <c:forEach items="${imageList}" var="image"> <tr> <td width="100px">${image.name}</td> <td width="250px">${image.description}</td> <td width="250px">${image.content}</td> </tr> </c:forEach> 

I really would appreciate it if someone could help me. Thanks

0
source share
1 answer

The problem is your understanding of how HTTP and HTML work. When a web page contains an image, HTML does not contact the contents of the image. All it contains is an img tag with an src attribute containing the URL of this image:

 <img src="/the/path/of/the/image.jpg"/> 

The browser sends the first request to receive the HTML page. Then it parses the HTML, sees the img tag, and sends a second request to the image URL to get its contents (bytes) and display it.

Your code is trying to generate an HTML page containing a blob (in fact, the result of calling toString () on the blob) directly on the page:

 <td width="250px">${image.content}</td> 

This is not true.

What you need to generate is an image tag whose src attribute contains the action URL that will receive image bytes from the database and send them to the browser, i.e. img tag indicating the loading action:

 <td width="250px"><img src="<c:url value='/download/${image.id}'/>"/></td> 
0
source

Source: https://habr.com/ru/post/976521/


All Articles