If the download was canceled, the browser will close the connection, which will result in an IO exception on the servlet side. For example, in Tomcat it will say “Connection reset by peer”, and this is a ClientAbortException exception. Other servers exchange IOException in different ways. The point simply catches an IOException, and you should be able to handle it as you wish.
Using content lengths is not reliable because the HTTP specification does not require content length headers for POST - or for GET for this question. Point, if you are not sure that your Javascript XHR explicitly sets the header, this method will not work.
Alternatively, you can calculate it yourself and install it to make sure, or even better, add your own character stream to the end of the placed data in XHR, some unique character string, for example. 'jh923k49sk $ 2 #%'. In the server, disable the last 14 characters of the incoming message and check it for string. If this is the same, you know that they did not cancel.
source share