照片上传回显基于spring mvc的 MultipartHttpServletRequest

该博客介绍了一个基于Spring MVC的照片上传回显的方法。首先,在页面上添加img标签并设置id,然后通过window.open打开上传页面。上传成功后,将记录ID传到保存成功页面,再利用JavaScript修改img标签的src属性指向包含ID的action。最后,action根据接收到的ID查询照片并以流形式返回,完成回显。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:

在需要回显的地方加一个img 并加好id 如 <a href="#" onclick="uploadphoto()" ><img id="ppersonphoto" src="icasp/Style/wssb/xfrzp.gif"  /></a>

打开时用window.open打开一个上传附件的网页,

window.open(tourl,diatitle,'status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=400');

第二步:

在点击上传之后紧接着在action里跳到保存成功页面,并把当前保存的这条信息的id传过去。

第三步:

在保存成功页面接收id并把第一步的页面中的img传入一个action地址 

window.opener.document.getElementById("ppersonphoto").src="wssbperson.action?method=outimg&unid="+unid;

第四步:

传入之后在outimg这个action里接收到id去查出照片并通过流写出到第一步的img中 这样就达到了回显

public void outimg(HttpServletRequest req, HttpServletResponse res) throws Exception{
		String unid=req.getParameter("unid");
		WssbPersonphoto photo=commonService.get(WssbPersonphoto.class, unid);
		BufferedInputStream inputImage = null; 
		try {
			Blob blob =photo.getPphoto();
			OutputStream out = res.getOutputStream();
			inputImage = new BufferedInputStream(blob.getBinaryStream());
			byte[] contents = new byte[1024];
			int byteRead = 0;  
			String strFileContents;  
			while((byteRead = inputImage.read(contents)) != -1){  
				out.write(contents);
			}  

			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值