问题
I have this configured in my server.xml file
<Connector URIEncoding="UTF-8" connectionTimeout="20000" maxHttpHeaderSize="65536" **maxPostSize="1024"** port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
However, requests more than 1 KB pass as if maxPostSize is never set. Can anyone suggest what can cause that?
Another thing, I would like to know how to make a custom http reply from tomcat if the post size is more than 1 KB
UPDATE Since I have been for so long in this issue. I had the opportunity to look at the source code for tomcat to check what is happening here exactly: click here
I noticed from lines 2541 till 2550, they are using getContentLength() although the documentation says "maxPostSize: The maximum size in bytes". How come could this be in bytes? It looks more to characters count to me and it can be done in servlet side. Can someone explain what I am missing here?
回答1:
According to tomcat doc the maxPostSize is 2M.
The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.
If you would to change the default values then change in the file location below:
$CATALINA_HOME/conf/server.xml
Example:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="6291456" />
The size in bytes 6291456*(1024*1024)=6M
Note: Please make sure you restart the server after making the changes.
回答2:
By default, HTTP containers use inputstream of HTTP request object and process the data as bytes. If the request has any encoding , it will use character based reader object .
https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletRequest.html#getInputStream()
While sending response also, the content length are set in bytes. You set the content length as bytes and either write as character or bytes.
Servers will handle the data as bytes
来源:https://stackoverflow.com/questions/42470475/how-to-limit-post-size-in-tomcat-8-how-to-make-a-custom-reply-for-it