如何只处理 ClientAbortException?
How to handle only ClientAbortException?
我正在编写一个下载文件控制器
有时用户会在文件完全写入之前关闭浏览器 window。 - 这很酷。
问题是我的日志中充满了这个错误:
org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:333)
at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:758)
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:663)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:368)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:346)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2147)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
当我试图只捕捉这个特定的错误时,eclipse 说:
ClientAbortException cannot be resolved to a type
我有项目设置并且 运行 正确所以是否可以只捕获这个特定的异常:
org.apache.catalina.connector.ClientAbortException
我想保留 IOException 以防再次发生灾难。
ClientAbortException
派生自IOException
。您必须检查到底是什么异常导致 IOException e
:
...
} catch (FileNotFoundException fnfe) {
// ... handle FileNotFoundException
} catch (IOException e) {
String exceptionSimpleName = e.getCause().getClass().getSimpleName();
if ("ClientAbortException".equals(exceptionSimpleName)) {
// ... handle ClientAbortException
} else {
// ... handle general IOException or another cause
}
}
return null;
而不是寻找某个 class 名称(将您的应用程序绑定到特定的 servlet 容器)我通常处理 IOExceptions
写入时不同于读取时的 IOExceptions,就像这样(非常 pseudo-ish代码):
try {
byte[] buffer = ...
in.read(buffer);
try {
out.write(buffer);
} catch (IOException writeException) {
// client aborted request
}
} catch (IOException readException) {
// something went wrong -> signal 50x or something else
}
到目前为止还算不错。
(截至@Nikolas Charalambidis 的回答)
忽略"org.apache.catalina.connector.ClientAbortException:java.io.IOException: Connection reset by peer"
e.getCause().getClass().getSimpleName()
== "IOException"
e.getMessage()
== "java.io.IOException: Connection reset by peer"
并且不处理一般的 IOException 和其他原因
...
} catch (IOException e) {
if (! e.getMessage().contains("Connection reset by peer")) {
throw e;
}
} finally {
close(output);
close(input);
}
...
我正在编写一个下载文件控制器
有时用户会在文件完全写入之前关闭浏览器 window。 - 这很酷。
问题是我的日志中充满了这个错误:
org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:333) at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:758) at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:663) at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:368) at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:346) at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96) at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2147) at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102) at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123) at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
当我试图只捕捉这个特定的错误时,eclipse 说:
ClientAbortException cannot be resolved to a type
我有项目设置并且 运行 正确所以是否可以只捕获这个特定的异常:
org.apache.catalina.connector.ClientAbortException
我想保留 IOException 以防再次发生灾难。
ClientAbortException
派生自IOException
。您必须检查到底是什么异常导致 IOException e
:
...
} catch (FileNotFoundException fnfe) {
// ... handle FileNotFoundException
} catch (IOException e) {
String exceptionSimpleName = e.getCause().getClass().getSimpleName();
if ("ClientAbortException".equals(exceptionSimpleName)) {
// ... handle ClientAbortException
} else {
// ... handle general IOException or another cause
}
}
return null;
而不是寻找某个 class 名称(将您的应用程序绑定到特定的 servlet 容器)我通常处理 IOExceptions
写入时不同于读取时的 IOExceptions,就像这样(非常 pseudo-ish代码):
try {
byte[] buffer = ...
in.read(buffer);
try {
out.write(buffer);
} catch (IOException writeException) {
// client aborted request
}
} catch (IOException readException) {
// something went wrong -> signal 50x or something else
}
到目前为止还算不错。
(截至@Nikolas Charalambidis 的回答)
忽略
"org.apache.catalina.connector.ClientAbortException:java.io.IOException: Connection reset by peer"
e.getCause().getClass().getSimpleName()
== "IOException"e.getMessage()
== "java.io.IOException: Connection reset by peer"并且不处理一般的 IOException 和其他原因
... } catch (IOException e) { if (! e.getMessage().contains("Connection reset by peer")) { throw e; } } finally { close(output); close(input); } ...