springmvc项目导出excel时碰到cookie/flash.js劫持,初次请求返回结果如下:

    1. <html>
    2. <head>
    3. <script language="javascript">setTimeout("location.replace(location.href.split(\"#\")[0])",2000);</script>
    4. <script type="text/javascript" src="http://10.48.0.250:89/cookie/flash.js"></script>
    5. <script language="javascript">setURL("10.48.0.250");supFlash("1985617644");</script>
    6. </head>
    7. <body>
    8. <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="0" height="0" id="m" align="center"><param name="allowScriptAccess" value="always" />
    9. <param name="movie" value="http://10.48.0.250:89/cookie/flashcookie.swf" />
    10. <param name="quality" value="high" />
    11. <param name="FlashVars" value="srv=10.48.0.250" />
    12. <embed src="http://10.48.0.250:89/cookie/flashcookie.swf"FlashVars="srv=10.48.0.250" quality="high" width="0" height="0" name="m" align="center" allowScriptAccess="always" type="application/x-shockwave-flash"pluginspage="http://www.macromedia.com/go/getflashplayer" />
    13. </object>
    14. </body></html>

    初次请求被劫持返回这个页面之后又触发了两次请求,导致浏览器弹出两次文件下载窗口。猜想初次请求已经到了后端,然后上面这个html里的setTimeout脚本又请求了一次后端。所以这里改用iframe加载导出链接,然后屏蔽iframe的setTimeout方法的方式,避免html里的二次请求。代码如下:

    1. var url = "/declare/exportExcel";
    2. if ($('#iframeExcel').eq(0) && $('#iframeExcel').eq(0).attr('src')) {
    3. $('#iframeExcel').attr('src', url);
    4. } else {
    5. var iframeExcel = "<iframe id='iframeExcel' src='" + url + "' width='0px' height='0px'></iframe>"
    6. $("body").append(iframeExcel)
    7. $('#iframeExcel')[0].contentWindow.setTimeout = function(){};
    8. }