简体   繁体   English

Java Servlet-下载并转发到页面

[英]Java Servlet - Download and Forward to a page

I am trying to send a download and then forward to a page. 我正在尝试发送下载,然后转发到页面。 but once the download completes the forward doesn't happen. 但是一旦下载完成,转发就不会发生。

That's normal behavior. 这是正常现象。 A file download will write the content of the file into the response and close the response stream, and a forward will try to write new content on the already closed response, resulting in nothing new being written in the response. 下载文件会将文件内容写入响应并关闭响应流,而转发文件将尝试在已关闭的响应上写入新内容,从而导致响应中没有新内容写入。

Solution: 解:

In your client, use JavaScript to fire the file download, and also fire a request to the page you want to forward. 在您的客户端中,使用JavaScript触发文件下载,并向您要转发的页面触发请求。

Client code adapted from here: Download a file and redirect...or alternative 从此处改编的客户端代码: 下载文件并重定向...或替代方法

<script>
function thanks() {
    setTimeout(function () {
        document.location.pathname = "another.jsp";
    }, 1000);
}
</script>

<a href="${request.contextPath}/yourServlet?file=foo.dat" onclick="thanks()">Download now!</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM