jsp页面显示图片

jsp页面显示图片自然是用img标签,但是src如果直接指向图片的路径,当添加图片后去查看是显示不出来的,因为没有加载到项目中,这里提供一个将图片以二进制流的方式传递的方法。

前端代码:

src指向后台io流读取图片方法,我这里的imgId是拼接的图片名,因为我们上传时的图片名有标准规格,路径是服务器指定路径。

<img src="<%=appPath%>/geReqAction/checkImgg.do?reqId=${reqId}&imgId=${reqId}" alt="ͼƬ1" id="jq22">

后台代码:

@RequestMapping("/checkImgg")
	public void checkImgg(@Param("reqId")String reqId,@Param("imgId")String imgId,ModelAndView mav,HttpServletResponse response,HttpServletRequest request) throws IOException{
		mav.addObject("reqId",reqId);
		mav.addObject("imgId",imgId);
		String contextPath = request.getSession(). getServletContext().getRealPath("/"); 
		//存储请求参数
		//PDF字节流转为PDF显示页面
		ServletOutputStream output=response.getOutputStream();
		//获取返回的PDF字节流
		FileInputStream in=null;
		File file = new File(contextPath+"/themes/default/images/reqSystem/"+reqId+"/"+imgId+".jpg");
		try {
			in = new FileInputStream(file);
			byte[] bytearray = new byte[1024];
			int size;
			while((size=in.read(bytearray))!=-1){
				output.write(bytearray, 0,size);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(in!=null){
				in.close();				
			}	
			output.close();
		}
	}

 

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值