Java HttpServletResponse将多个图像写入OutputStream(在同一查询中)
以下是我想做的: 在我的spring控制器中,当收到请求时,我会动态生成一张图表。 我想在生成图表时,在HttpServletResponse的OutputStream中设置一个类似“加载时请稍候”的图像 现在的情况是浏览器接收到“加载时请稍候”,但从未更新到实际图表。我调试了代码,图表被写入OutputStream,但浏览器没有加载它 我不能有客户端脚本 谢谢Java HttpServletResponse将多个图像写入OutputStream(在同一查询中),java,spring,servlets,Java,Spring,Servlets,以下是我想做的: 在我的spring控制器中,当收到请求时,我会动态生成一张图表。 我想在生成图表时,在HttpServletResponse的OutputStream中设置一个类似“加载时请稍候”的图像 现在的情况是浏览器接收到“加载时请稍候”,但从未更新到实际图表。我调试了代码,图表被写入OutputStream,但浏览器没有加载它 我不能有客户端脚本 谢谢 @RequestMapping("/img/**") public void img(HttpServletRequest reque
@RequestMapping("/img/**")
public void img(HttpServletRequest request, HttpServletResponse response, ChartRequest request) {
ServletOutputStream os = null;
try {
os = response.getOutputStream();
response.setContentType("image/png");
os.write(base64PNGToByte(ImageContainer.PNG_PLEASE_WAIT)); // here is the "PLEASE WAIT WHILE LOADING" image
os.flush(); // browser receives the image
ChartResponse chartResponse = chartManager.processChart(request);
if (!chartResponse .isSuccess()) {
os.write(base64PNGToByte(null));
return;
}
String base64Img = chartResponse .getResponse();
os.write(base64PNGToByte(base64Img)); // the generated chart image, but browser does not shows this image...
} else {
os.write(base64PNGToByte(null));
}
os.flush();
} catch (IOException e) {
logger.debug("img: IOException ", e);
}
}
servlet不能像您所希望的那样生成两个不同的响应 但是,使用
(样式可能需要调整,但按照这些思路进行调整是可行的)干净、简单。谢谢
<img src="your/dynamic/url?data=123"
style="background-image: url('path/to static/please-wait.png');
background-position: 50% 50%; background-repeat: no-repeat;"
/>