我需要从MongoDB GridFS系统获取图像,然后将其显示在JSP img标记中。 这是我的代码,心不是工作:来自MongoDB的jsp中的春天显示图像
@RequestMapping(value = "/getPhoto", method = RequestMethod.GET)
public @ResponseBody
void getPhoto(HttpServletRequest request,
HttpServletResponse response) {
try {
System.out.println("getting photo...");
GridFSDBFile imageForOutput = userFacade.loadProfilePhoto((User) SecurityContextHolder.getContext().getAuthentication()
.getPrincipal());
BufferedImage image = ImageIO.read(imageForOutput.getInputStream());
byte[] imageBytes = ((DataBufferByte) image.getData().getDataBuffer()).getData();
response.setHeader("expires", "0");
response.setContentType("image/jpg");
response.setContentLength(imageBytes.length);
OutputStream out = response.getOutputStream();
out.write(imageBytes, 0, imageBytes.length);
out.flush();
out.close();
return;
} catch (Exception e) {
// TODO Auto-generated catch block
}
首先我得到的GridFSDBFile,然后我需要得到的byte []之后,我把它写在响应对象,但我不知道如果我这样做。正确。
在JSP中的代码如下:
<c:url var="getPhoto" value="/settingsAdmin/getPhoto" />
<div id="preview">
<img id="imagePreview" src="${getPhoto}" alt="Profile Photo"/>
</div>
最后,控制器被正确调用,但这个错误必须在它里面。
THX提前
这里的方法不是很好。你应该**不要**在你的标记中嵌入图像数据。您**应该**使用控制器端点来模拟文件。一般的方法显示[这里](http://stackoverflow.com/a/22400508/2313887) –
不知道太多关于JSP。错误究竟是什么?你看不到图像或什么?如果要将图像数据嵌入到标记中,请使用[示例](http://stackoverflow.com/questions/1207190/embedding-base64-images)。但绝对不是一个好主意。 – yaoxing
问题是img没有显示在视图中,是否有从GridfsDbFile中获取Base64字符串以便至少在视图中显示它,它是我第一次这样做 – guille209