【问题标题】:Displaying a blob image in a jsp page using Spring MVC 3使用 Spring MVC 3 在 jsp 页面中显示 blob 图像
【发布时间】:2011-10-21 20:50:42
【问题描述】:

我有一个类(其中包括)从数据库中检索的字段图片:

public class Person {
  String name;
  Blob picture;
}

然后我有一个控制器,我可以在其中将人员对象添加到模型中

@RequestMapping(value = "/online", method = RequestMethod.GET)
public String getCurrentUser(Model model) {
  Person person = getMyPerson()
  model.addAttribute("person", person);
  return "online";
}

最后我有一个 .jsp 页面来显示用户:

<html>
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   ..some tags

   Persons name: ${person.name}
   Persons picture: ?your_answer_here? 
</html>

所以问题(显然)是如何将 blob 字段显示为图像?我已经尝试过但失败了。我真的不想对数据库执行新查询,我只想显示我已经拥有的图像..

【问题讨论】:

    标签: spring jsp spring-mvc


    【解决方案1】:

    HTTP 不允许直接这样做。每个图像都需要是对单独 URL 的单独请求。因此,您需要一个控制器/servlet,它接受用户 ID 作为参数并将 blob 写入响应流(并适当地设置 Content-Type 标头 - image/jpeg、image/png 等)

    【讨论】:

    • 谢谢!实现了一个用于输出图像的新控制器
    猜你喜欢
    • 2013-12-20
    • 2012-02-24
    • 2012-04-20
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多