如何使用spring mvc从数据库中获取图像

时间:2015-07-19 11:31:58

标签: java spring jsp spring-mvc

我尝试在我的jsp上获取图像,但它不起作用.. JSP没有显示图像。使用SpringMVC,Spring Data(JpaRepository),jdbc。 PostgreSQL和HSQL(两者都不起作用)

我的代码:

实体:

@Entity
@Table(name = "Images")
public class Image {

@Id
@GeneratedValue
private Integer id;

private String name;

@Lob
@Basic(fetch=FetchType.LAZY)
private byte[] body;

控制器:

@RequestMapping(value = "/image/imageDisplay", method =     RequestMethod.GET)
public void showImage(@RequestParam("id") Integer id,  HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{

        Image image = bookService.getImage(id);
        response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
        response.getOutputStream().write(image.getBody());
        response.getOutputStream().close();


}

服务:

public Image getImage(int id) {
    return imageRepository.findOne(id);
}

JSP:

<img src="/image/imageDisplay?id=${book.image.id}" class="media-object pull-left">

我将非常感谢您的帮助!谢谢。

0 个答案:

没有答案