后端代码:
@RequestMapping(value = "read")
public void index(Model model, HttpServletRequest req,
HttpServletResponse resp, String attachmentId) {
byte [] bytes = null;
ServletOutputStream sos =null;// 将图像输出到Servlet输出流中。
try {
GetImageRes getImageRes = acctManager.getImageBytes(attachmentId);
bytes = getImageRes.getFilebyte();
ByteArrayInputStream in = new ByteArrayInputStream(bytes); //将b作为输入流;
BufferedImage image = ImageIO.read(in);
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
sos = resp.getOutputStream();// 将图像输出到Servlet输出流中。
ImageIO.write(image, "jpeg", sos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
直接访问springmvc路径 直接访问通过流的形式返回图片,但是这个有一个缺陷就是 返回的title是路径名称加像素。
为了解决这种问题 从前端改接受流的方式完成。
前端代码:
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=0.1">
<title>图片</title>
</head>
<body style="margin: 0px;">
<img id="tempImage" style="-webkit-user-select: none;cursor: zoom-in;" src="$!imgUrl" width="100%" height="100%">
</body>
</html>
后端代码:
@RequestMapping(value = "readHtml")
public String readHtml(Model model, HttpServletRequest req,
HttpServletResponse resp, String attachmentId,String recommendId) {
model.addAttribute("imgUrl", "这里直接写上访问流路径");
return "tempImage/tempImage";
}
这里就完成了 。