【问题标题】:Removing Content-Disposition causes ClientAbortException: java.net.SocketException: socket write error: Connection aborted by peer删除 Content-Disposition 会导致 ClientAbortException: java.net.SocketException: socket write error: Connection aborted by peer
【发布时间】:2015-08-19 10:38:32
【问题描述】:

我的应用程序服务器 .wav 文件,可在特定 URL 下载。我必须更改逻辑,以便将它们流式传输而不是下载 - 所以我将删除明确设置的 Content-Disposition 标头。

一段代码:

// removed    
//response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

bis = new BufferedInputStream(inputStream);
bos = new BufferedOutputStream(sOutputStream);

byte[] buff = new byte[10000];
int bytesRead = 0;
while(-1 != (bytesRead = bis.read(buff))) {
    bos.write(buff, 0, bytesRead);
}
bos.flush();

第二次或第三次调用bos.write 原因

ClientAbortException: java.net.SocketException: socket write error: Connection aborted by peer 
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:402)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:449) 
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:349) 
    at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:425) 
    at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:414) 
    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89) 
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105) 

当我调试代码时,当写入方法失败时,浏览器打开一个播放器,另一个相同的请求正在生成并成功。

设置 Content-Disposition 后,一切正常。有什么想法吗?

【问题讨论】:

    标签: apache servlets socketexception content-disposition


    【解决方案1】:

    这是因为客户端在注意到它实际上是一个媒体文件后中止请求并通过客户端的媒体播放器通过HTTP Range请求切换到流模式以提高缓冲速度。然后,客户端将在文件的不同部分触发多个 HTTP 请求(显然,只有当您的 servlet 也真正支持它时,这才有效……许多自制的文件 servlet 不这样做,并且最终可能会执行得更糟)。

    对于服务器日志中的那些客户端中止异常,最好的办法是过滤掉并抑制它们,或者至少使用 DEBUG/INFO oneliner 而不是整个堆栈跟踪来记录。

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-24
      • 2019-02-02
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多