对于此答案,我将假定“纯” JSP / Servlet / HTML /
JS,因为我不使用Struts。对于高级Struts(和jQuery)用户来说,将其移植到Struts(和jQuery)应该足够简单。
到目前为止,您可以在下载请求的响应上设置一个cookie,并让Javascript轮询该cookie。一旦准备好提供下载,该cookie将在Javascript中可用。为了确保在同一会话中跨各种浏览器窗口/选项卡工作,最好是生成一个唯一的下载令牌,并将其作为请求参数传递回去,以便服务器端可以将其设置为cookie值。不要忘了让cookie过期,以防止cookie污染。
基本上(您可以
<span>用
<img>指向一些微调gif的方式代替):
<input type="button" value="Download" onclick="download()" /><span id="wait" >Please wait while we prepare the download...</span>
使用此Javascript(使用jQuery时,jquery-cookie插件可能会有所帮助):
function download() { var token = new Date().getTime(); var wait = document.getElementById("wait"); wait.style.display = "block"; var pollDownload = setInterval(function() { if (document.cookie.indexOf("download=" + token) > -1) { document.cookie = "download=" + token + "; expires=" + new Date(0).toGMTString() + "; path=/"; wait.style.display = "none"; clearInterval(pollDownload); } }, 500); window.location = "download?token=" + token;}并在servlet中(或适用的Struts动作):
// Prepare download here.// ...// once finished, set cookie and stream download to response.cookie cookie = new cookie("download", request.getParameter("token"));cookie.setPath("/");response.addcookie(cookie);// ...


