【发布时间】:2015-01-28 21:48:31
【问题描述】:
有人可以帮忙吗,我正在尝试使用 Spring MVC + Hibernate 从数据库中检索带有一些文本的图像,但失败得很惨。我可以显示一个或另一个,但不能同时显示(图像和文本),它也在控制台中给我一个错误。
错误:- getOutputStream() has already been called for this response DAO:-
public List<Product> listProductsById(Integer productId) {
List<Product> prod = em.createNamedQuery("Product.findByProductId")
.setParameter("productId", productId).getResultList();
return prod;
}
public byte[] loadImage(Integer productId){
return em.find(Product.class, productId).getimage();
}
控制器:-
@RequestMapping(value = "details/{id}", method = RequestMethod.GET)
public String showProductDetails(@PathVariable("id") Integer id,
Model model, HttpServletResponse response) throws IOException {
List<Product> product = olss.listProductById(id);
byte[] image = olss.loadImage(id);
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
response.getOutputStream().write(image);
response.getOutputStream().flush();
response.getOutputStream().close();
model.addAttribute("prodList", product);
model.addAttribute("prodList", image);
return "prodDetails";
}
JSP:-
<img alt="Image of product" src="prodDetails?id=${prod.productId}">
【问题讨论】:
-
在数据库中存储图像可能不是一个好习惯。如果可能,只需将其存储在服务器上并将路径存储在数据库中。
标签: java mysql spring hibernate spring-mvc