问题:

httpclient 向sringboot写的服务端上传文件,上传小文件没有问题,但是上传大文件,服务端就会报SocketTimeoutException错误

2021-06-17 19:32:09.992 default [http-nio-0.0.0.0-8080-exec-10] ERROR com.example.demo.service.FileService - Line:1193 - 捕获异常:org.apache.catalina.connector.ClientAbortException: java.net.SocketTimeoutException,异常信息:java.net.SocketTimeoutException,异常原因:{},
java.net.SocketTimeoutException: null
	at org.apache.tomcat.util.net.NioBlockingSelector.read(NioBlockingSelector.java:204)
	at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:221)
	at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1220)
	at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1137)
	at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:735)
	at org.apache.coyote.http11.Http11InputBuffer.access$300(Http11InputBuffer.java:41)
	at org.apache.coyote.http11.Http11InputBuffer$SocketInputBuffer.doRead(Http11InputBuffer.java:1070)
	at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:102)
	at org.apache.coyote.http11.Http11InputBuffer.doRead(Http11InputBuffer.java:246)
	at org.apache.coyote.Request.doRead(Request.java:551)
	at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:336)
	at org.apache.catalina.connector.InputBuffer.checkByteBufferEof(InputBuffer.java:632)
	at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:362)
	at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:132)
	at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:110)
	at java.nio.file.Files.copy(Files.java:2908)
	at java.nio.file.Files.copy(Files.java:3027)

具体异常说明参考:https://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/connector/ClientAbortException.html

原因:

该问题主要是因为服务端使用InputStream 读取客户端上传的文件,当上传大文件的时候用时比较长,超过了客户端或者服务端的设置的超时时间,而导致socket连接的一端关闭了socket连接,服务端还没有从InputStream中将上传文件读取完,就会出现读取InputStream异常,SocketTimeoutException。

所以主要原因是因为上传文件用时较长,超过了超时时间,具体需要看是客户端设置的响应超时(http.socket.timeout)太小,还是服务端设置的连接超时(connectionTimeout)太短,然后增大超时时间。

造成该问题的其他原因参考:https://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset

解决:

1.如果是客户响应超时时间太小,增大httpclient的响应超时时间(http.socket.timeout)

以httpclient为例,connect.setSocketTimeout(6000)   单位ms

2.如果是服务端的连接超时时间太短,增大server的连接超时时间(connectionTimeout)

以springboot Tomcat为例,在配置文件增加  server.tomcat.connectionTimeout=-1

-1  表示,无限时间,不会超时,Tomcat具体超时配置项参考如下:

socket.soTimeout

This is equivalent to standard attribute connectionTimeout.

connectionTimeout

The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless disableUploadTimeout is set to false, this timeout will also be used when reading the request body (if any).

connectionUploadTimeout

Specifies the timeout, in milliseconds, to use while a data upload is in progress. This only takes effect if disableUploadTimeout is set to false.

disableUploadTimeout

This flag allows the servlet container to use a different, usually longer connection timeout during data upload. If not specified, this attribute is set to true which disables this longer timeout.

tomcat其他配置项参考:http://tomcat.apache.org/tomcat-10.0-doc/config/http.html

参考:

https://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset

https://stackoverflow.com/questions/839314/clientabortexception-java-net-socketexception-connection-reset-by-peer-socket

https://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/connector/ClientAbortException.html

http://tomcat.apache.org/tomcat-10.0-doc/config/http.html

https://www.cnblogs.com/tv151579/p/6124923.html

https://help.aliyun.com/knowledge_detail/50731.html

https://blog.csdn.net/abcdu1/article/details/117373889

https://www.cnblogs.com/buguge/p/8297630.html

https://blog.csdn.net/h254532699/article/details/54342470/

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐