首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springMVC 返回流图片显示,前端接受流

springMVC 返回流图片显示,前端接受流

作者头像
斯文的程序
发布2019-11-07 19:22:15
1.3K0
发布2019-11-07 19:22:15
举报
文章被收录于专栏:带你回家带你回家

后端代码:

	@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";
	}

这里就完成了 。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档