maxSwallowSize 的真正作用是什么?

What does maxSwallowSize really do?

我有一个 Spring 引导后端,我刚刚解决了“ERR_CONNECTION_RESET”,当从 Angular 前端上传文件时,通过配置 maxSwallowSize Tomcat 属性。我试图了解它到底做了什么。 Tomcat 文档对我来说不是很明显:

The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored but the client still sends it. If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2097152 (2 megabytes) will be used. A value of less than zero indicates that no limit should be enforced.

https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

我能得到一些帮助吗?

如果你的 servlet 的 service() method exits (normally or exceptionally) without consuming the whole client's request body, Tomcat will still accept maxSwallowSize bytes before resetting the connection. This is required since most browsers read the server's response only after they sent the entire request (cf. this question).

要使用请求正文,您需要:

  • 如果请求编码为application/x-www-form-urlencoded,则需要调用getParameter*方法之一,
  • 如果请求编码为multipart/form-data,则需要调用getPart*方法之一,
  • 在所有其他情况下,您需要阅读整个 InputStream

未使用的请求体通常是由错误引起的,包括在解析参数或表单部分时的错误。