有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解决“对等连接重置:socket写入错误”?

当我从服务器读取文件内容时,它返回以下错误消息:

Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:119)
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
at org.apache.coyote.Response.doWrite(Response.java:504)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:383)
... 28 more

我的servlet程序是

 response.setContentType("application/octet-stream");
 response.setHeader("Content-Disposition","attachment;filename="+filename);
 FileInputStream in = new FileInputStream(new File(filepath));
 ServletOutputStream output=response.getOutputStream();
 byte[] outputByte=new byte[4096];
 while(in.read(outputByte,0,4096)!=-1){
     output.write(outputByte,0,4096);//error indicates in this line
 }
 in.close();
 output.flush();
 output.close();

如何解决这个问题


共 (5) 个答案

  1. # 1 楼答案

    看来你的问题可能出现在

    while(in.read(outputByte,0,4096)!=-1){

    它可能会因为不推进偏移量而进入无限循环(在调用中始终为0)。试试看

    while(in.read(outputByte)!=-1){

    默认情况下,它将尝试读取高达outputByte的数据。插入byte[]的长度。这样你就不用担心偏移量了。见FileInputStrem's read method

  2. # 2 楼答案

    正确的“解决”方法是关闭连接,忘记客户端。客户已经关闭了连接,而你还在给它写信,所以他不想认识你,就这样,不是吗

  3. # 3 楼答案

    我也有同样的例外,在我的情况下,问题是在重新谈判过程中。事实上,当服务器试图更改密码套件时,我的客户端关闭了一个连接。挖掘之后,JDK1.6更新22renegotiation process is disabled by default。如果您的安全约束可以做到这一点,请尝试通过将sun.security.ssl.allowUnsafeRenegotiation系统属性设置为true来启用不安全的重新协商。以下是有关该过程的一些信息:

    Session renegotiation is a mechanism within the SSL protocol that allows the client or the server to trigger a new SSL handshake during an ongoing SSL communication. Renegotiation was initially designed as a mechanism to increase the security of an ongoing SSL channel, by triggering the renewal of the crypto keys used to secure that channel. However, this security measure isn't needed with modern cryptographic algorithms. Additionally, renegotiation can be used by a server to request a client certificate (in order to perform client authentication) when the client tries to access specific, protected resources on the server.

    此外,还有关于这个问题的详细信息,并以(IMHO)可理解的语言书写

  4. # 4 楼答案

    客户端(浏览器)已关闭套接字

    代码中的一个错误:

    byte[] outputByte=new byte[4096];
    while(in.read(outputByte,0,4096)!=-1){
       output.write(outputByte,0,4096);
    }
    

    最后一次数据包读取,然后写入可能有一个长度<;4096,所以我建议:

    byte[] outputByte=new byte[4096];
    int len;
    while(( len = in.read(outputByte, 0, 4096 )) > 0 ) {
       output.write( outputByte, 0, len );
    }
    

    这不是你的问题,但这是我的答案…;-)

  5. # 5 楼答案

    我也有同样的问题,但差别很小:

    Exception was raised at the moment of flushing

    这是一个不同的stackoverflow issue。简短的解释是错误的响应标题设置:

    回应。setHeader(“内容编码”、“gzip”)

    尽管未压缩响应数据内容

    因此,浏览器关闭了连接