问题描述
如何判断用户在从Java Servlet下载过程中是否被取消?似乎对我来说,IE,输出流println()阻塞。使用的命中取消,但是有没有办法时间这样或这样的事情?
How can you tell if the user hit cancel during a download from a Java servlet? What seem to happen for me with IE, the output stream println() blocks. The used hit cancel, but is there a way to time this out or anything like that?
推荐答案
包裹 OutputStream#write()
, #flush()
和 #close()
try-catch
阻止 IOException
。如果被捕获,那么通常意味着最终用户已经中止请求。
Wrap OutputStream#write()
, #flush()
and #close()
in a try-catch
block on IOException
. If caught, then it usually means that the enduser has aborted the request.
为了进一步了解,您还可以捕获更多的servlet容器特定异常,如在Tomcat和克隆(这是 IOException
的子类),但这将使您的webapp代码不能与其他servlet容器。
To get a step further, you could also catch on a more servletcontainer-specific exception like ClientAbortException
on Tomcat and clones (which is a subclass of IOException
), but this will make your webapp code unportable to other servletcontainers.
更新:要回覆屏蔽问题,您可以致电 flush()
在写入单个数据块之后。这将使实际将数据发送给客户端,并在底层套接字关闭时导致 IOException
。
Update: to come back on the blocking issue, you'd like to call flush()
after writing a single block of data. This will actually send the data to the client and cause IOException
when the underlying socket is closed.
这篇关于如何判断用户在从Java servlet下载过程中是否被取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!