SpringBoot项目在接收参数时,突然出现Unexpected EOF read on the socket异常报错,此时请求还没有进入服务端,初步看起来是客户端的问题。
2022-06-10 11:53:36 [http-nio-10244-exec-3] WARN o.s.w.s.m.support.DefaultHandlerExceptionResolver.logException 194 --> Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.EOFException: Unexpected EOF read on the socket]
2022-06-10 11:54:06 [http-nio-10244-exec-5] WARN o.s.w.s.m.support.DefaultHandlerExceptionResolver.logException 194 --> Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.EOFException: Unexpected EOF read on the socket]
进行Wireshark抓包查看HTTP请求报文,发现只有服务端响应报文,客户端请求报文丢失了。
请求方式是POST请求参数是String类型,不应该出现接收不到的情况,部署在其他环境上是好的。
@RequestMapping(value = "/getTest", method = RequestMethod.POST)
public void getTest(@RequestBody String paramter) {
log.info(paramter);
}
于是我又换了另外一种请求方式,使用request接收,然后通过读取reader的方式获取参数,此时仍然报错,但是查看读取到的数据里面是有参数的,只是参数不完整,无法解析通过JSON转换,此情况表示请求已经接收到了,但是由于某原因导致吧请求放弃停止了。
@RequestMapping(value = "/getTest", method = RequestMethod.POST)
public void getTest(HttpServletRequest request) {
char[] lineChars = new char[1024 * 1024];
char[] totalChars = new char[1024 * 1024];
int readLen = 0;
int totalLen = 0;
try {
BufferedReader reader = request.getReader();
while ((readLen = reader.read(lineChars)) > 0) {
for (int i = 0; i < readLen; i++) {
totalChars[totalLen + i] = lineChars[i];
}
totalLen += readLen;
}
} catch (Exception e) {
log.error("数据读取错误{}", e.getMessage());
return;
}
}
由于参数有图片Base64数据,怀疑可能数据包过大导致tomcat服务器接不完全导致,于是增加配置参数,让Tomcat服务器不限制大小接收POST接口的数据包,默认为10M。
server.tomcat.max-http-post-size=-1
修改后替包重启,问题得到了解决。