如何将文本输出流式传输到浏览器中的页面以显示可能需要大约15-20秒的操作进度?我试过直接写入HttpServletResponse
的输出流,但用户仍然看到完整输出后整个过程完成。如何从Java Spring MVC 3.0 Web应用流文本响应
这是我到目前为止已经试过
@RequestMapping(value = "/test")
public void test(HttpServletResponse response)
throws IOException, InterruptedException {
response.getOutputStream().println("Hello");
response.getOutputStream().flush();
Thread.sleep(2000);
response.getOutputStream().println("How");
response.getOutputStream().flush();
Thread.sleep(2000);
response.getOutputStream().println("are");
response.getOutputStream().flush();
Thread.sleep(2000);
response.getOutputStream().println("you");
response.getOutputStream().flush();
}
您是否在每次发送数据卡片后尝试刷新输出流? – Luciano 2012-07-05 14:49:31
@Luciano我已经添加了我到现在为止所尝试的问题。当我从Chrome查看页面时,这不起作用 – Danish 2012-07-05 14:59:13
我认为你最好的选择是一种不同的方法:在JavaScript中使用计时器,每隔1秒(或半秒)从/测试数据,使用ajax和/测试会返回到被调用点之前未发送的内容。 – Luciano 2012-07-05 15:04:02