JAVA开发示例:Spring MVC中如何设置图片类型的响应
功能需求
有些图片可能是存在MongDB中,需要JAVA获取之后返回,如果不指定类型,前台展现会有问题。
示例代码
response.setContentType("image/png");
@GetMapping(value="/bpmn")
public void getBpMnDiagram(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("image/png");
try (
InputStream imageStream = null; //获取图片输入流
ServletOutputStream fos = response.getOutputStream();
){
int i = 0;
byte[] buffer = new byte[4096];
while ((i = imageStream.read(buffer)) != -1) {
fos.write(buffer, 0, i);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
本文来自博客园,作者:白首码农,转载请注明原文链接:https://www.cnblogs.com/bsmn/p/16045207.html