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

本文介绍了一种解决直接访问SpringMVC路径返回图片时,图片title显示为路径名称加像素的问题的方法。通过修改后端代码,将图片信息封装并传递给前端,前端再以流的形式接收并展示图片,有效避免了title显示不规范的问题。

后端代码:

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

这里就完成了 。

转载于:https://my.oschina.net/960823/blog/3000335

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值