【发布时间】:2012-10-17 13:26:51
【问题描述】:
我正在尝试读取来自数据库的 blob 类型图像。我在控制器中的方法。 JSP 上仅显示图像文件
@RequestMapping(value = "/showDetails")
public ModelAndView showDetails(@RequestParam("doc") int id,
HttpServletResponse responce) {
ModelAndView mView = new ModelAndView();
File file = documentDao.getFileDetail(id);
byte[] bytes = null;
try {
OutputStream op = responce.getOutputStream();
int length = (int) file.getContent().length();
bytes = file.getContent().getBytes(1, length);
op.write(bytes);
op.flush();
op.close();
responce.setContentType("image/gif");
mView.addObject("image", op);
} catch (Exception e1) {
e1.printStackTrace();
}
mView.addObject("file", file);
mView.setViewName("filedetails");
return mView;
}
我的控制器类中的上述方法。我想在 JSP 上渲染图像以及一些文本。但是浏览器中只有图像。
【问题讨论】:
-
我无法格式化此代码。很抱歉在阅读/理解代码时遇到问题。
-
除了图片还想展示什么?
-
我想显示一些文本。 filedetails.jsp -->
---- -
@Slava Semushin 给出的答案是正确的。如果您的图像未呈现,则可能是它的路径不正确。例如。如果您在上下文中运行,则不应使用
/image?,而应使用/yourapp/image?(或更好-使用<c:url)