如何只处理ClientAbortException?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (1360)

我正在编写下载文件控制器 有时用户将在文件完全写入之前关闭浏览器窗口。 - 这很酷。

问题是我的日志充满了这个错误:

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​

提问于
用户回答回答于

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;

扫码关注云+社区

领取腾讯云代金券