JSP页面:
页面上接收数据流可以用<a>标签的“href”属性接收,也可以用<img>标签的“src”属性接收。
<a href="${ctx}/identify/img?imgId=${identify.img}" target="_blank">查看详情</a> (target="_blank",在新的标签页中打开)
接口:
@RequestMapping("img") public void getImg(HttpServletRequest request, HttpServletResponse response) throws Exception{ //照片存储在网盘中,获取页面传来的照片ID String id = request.getParameter("imgId"); //配置文件取url和身份验证token String url = ConfigReader.getProperty("CLOUDDISK_DOWNLOAD_URL"); String token = ConfigReader.getProperty("CLOUDDISK_AUTHCODE"); String param = "authCode=" + token + "&id=" + id; //从网盘下载照片的流 InputStream in = getImgStream(url,param,"GET"); //声明输出流,但是要从HttpServletResponse中获取才管用 BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream()); //从输入流到输出流 try { byte b[] = new byte[1024]; int len = in.read(b); while (len > 0) { bout.write(b, 0, len); len = in.read(b); } } catch (Exception e) { throw e; } finally { bout.close(); in.close(); } }