2013-06-29 48 views
0

我以某种方式设法在数据库中存储图像,但现在我想再次检索/显示图像。我只是不知道如何。基本上我的问题是如果我在我的ImageDao中缺少任何进一步的方法或者jsp中的配置应该如何。休眠 - 如何从数据库检索图像?

还有就是我的图片类:

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; 
     } 


} 

最后我的JSP的代码片段,我想 “访问” 了图像 - 但我失败了:

            <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> 

如果有人能帮助我,我真的很感激。谢谢

回答

0

问题在于你对HTTP和HTML如何工作的理解。当网页包含图像时,HTML不包含图像的内容。它包含所有为img标签,包含此图片的URL的src属性:

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

浏览器发送的第一请求获得HTML页面。然后它解析HTML,看到img标记,并向图像的URL发送第二个请求以获取其内容(字节)并显示它。

你的代码试图生成包含BLOB的HTML页面(事实上,调用toString()的Blob的结果)直接在页面:

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

这是不正确。

你需要生成什么是图像标签,其src属性包含动作将从数据库中获得的图像字节并将其发送到浏览器的URL,即一个img标签指向下载操作:

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

非常感谢你的回答。在您发表评论之后 - 以及其他一些问题 - 我不得不重新考虑我的整个项目。我按照你的说法调整了jsp,但图像不显示。我没有收到错误消息。这只是一个“x”。 – Nathalie