0
我正在构建一个使用spring启动和mongodb的blogging的web应用程序。我尝试使用gridFS存储和使用输入流来存储数据。但我不能回的图像作为模型attribute.It是我的控制器类如何在春季4中存储和返回图像作为模型属性
@RestController
public class HomeController {
@Autowired
GridFsTemplate gridFsTemplate;
@RequestMapping(value = "/home", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> getImage() {
GridFSDBFile gridFsFile = gridFsTemplate.findOne(new Query().addCriteria(Criteria.where("metadata.user").is("5")));;
InputStream inputStream = gridFsFile.getInputStream();
System.out.println("done");
return ResponseEntity.ok()
.contentLength(gridFsFile.getLength())
.contentType(MediaType.parseMediaType(gridFsFile.getContentType()))
.body(new InputStreamResource(gridFsFile.getInputStream()));
}
}
,这是我的看法
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<img th:src="@{/home}" />
</body>
</html>
欢迎SO。你应该发布一些你的代码尝试。否则,你会得到一堆倒票和评论,说这不是一个代码写作服务。 – BWMustang13
我添加了我的代码 –
只是为了清楚起见 - 你是**不是**在代码中的任何位置返回一个图像作为模型属性 - 你正在做的是将图像作为响应主体返回到'/ home'端点。当你在浏览器中打开'/ home'端点时会发生什么?图像显示出来了吗? – vtosh