从Servlet下载文件 - Chrome警告+下载后如何重定向用户?
我有一个带有downlod文件链接的简单表..
I have a simple table with downlod file links..
一切似乎工作正常,当我点击下载时,文件在硬盘上找到并送达用户。
Everything seems to work fine, when I click download, the file is found on the harddrive and served to the user.
我的问题是,当我检查铬时,我看到:
My problem is, when I inspect chrome I see:
Resource interpreted as Document but transferred with MIME type application/octet-stream: "http://localhost:8080/fus-app/myUploadedFiles".
为什么会这样说?
我的相关代码是:
httpServletResponse.setContentType("application/octet-stream");
httpServletResponse.setHeader("Content-Disposition", "attachment; filename=\"" + fileNameOnSystem + "\"");
还有一个红利问题:
之后文件已下载,用户仍然可以看到包含表格的页面,但我想重定向到其他页面。我怎样才能做到这一点?
After the file is downloaded, the user still sees the page with the table, but I want to redirect to a different page. How can I do that?
response.sendRiderect()
似乎不起作用。
编辑:这是我向用户提供下载链接的方式:
This is how I provide download link to the user:
<form method="post" action="<%= request.getServletContext().getContextPath() +"/myUploadedFiles" %>">
<input type="hidden" name="fileNameOnSystem" value="<%= rset.getString("fileNameOnSystem") %>" />
<button type="submit" class="btn btn-default" />Download File </button>
</form>
尝试在下载链接中指定HTML5下载属性:
Try specifying the HTML5 download attribute in your download link:
<a href='http://example.com/archive.zip' download>Export</a>
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download
至于重定向,这里不是好消息是否可以在提交响应后从servlet过滤器转发或重定向? .. 。
As for the redirect, not good news here Is it possible to forward or redirect from a servlet filter after the response has been committed? ...
HttpServletResponse的已提交状态实际上是
的一种方式,表示响应头是否已写入
底层套接字。 承诺响应已经(至少)写了第一个
行。由于响应的第一行包含状态
代码,因此您无法更改已提交的
响应的状态代码...这意味着将状态更改为3xx $为时已晚b $ b进行重定向。同样地,你不能做一个本地前锋,因为
你已经开始发送回复。
The "committed" status of an HttpServletResponse is really a way of saying whether the response headers have been written to the underlying socket. A "committed" response has had (at least) the first line written. Since the first line of the response contains the status code, it follows that you cannot change the status code of a committed response ... and that means it is too late to change the status to 3xx to do a redirect. Similarly, you cannot do a local forward because you've already started sending the response.
所以你不会在您发送文件(这是一个已提交的响应)后,能够对响应做更多的事情。但是,您可以先将它们转发到另一个页面,然后显示一条消息并最终触发下载。
So you won't be able to do anything more with the response after you've sent the file (which is a committed response). However, you could forward them onto another page first, and then have that display a message and eventually trigger the download.
找到此示例:
<html>
<head>
<meta http-equiv="refresh" content="5;url=http://server.com/file.zip">
</head>
<body>
Thank you for downloading file.zip!
</body>
</html>