简体   繁体   中英

ServletResponse.setBufferSize doesn't work in Tomcat 7?

I'm increasing the buffer size of a response using ServletResponse.setBufferSize but Tomcat 7 is still throwing an exception that the buffer size is not big enough. Is this a bug in Tomcat 7.0.32?

Here is my pseudo/code -

@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {

    if (some condition)
    {
        ((ServletServerHttpResponse)outputMessage).getServletResponse().setBufferSize(Integer.MAX_VALUE);
        outputMessage.getHeaders().set("Custom-Header", gson.toJson(big payload));
    }

    // ...

This is the exception -

org.apache.coyote.http11.HeadersTooLargeException : An attempt was made to write more data to the response headers than there was room available in the buffer. Increase maxHttpHeaderSize on the connector or write less data into the response headers.

Do I need to reset the buffer? reset() did not work.

EDIT: I'm looking for the ability to change the maximum header size at the individual response level based upon "some condition".

The message is about header size, not total buffer size. From the Tomcat Configuration Guide section on "The HTTP Connector"

maxHttpHeaderSize

The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 8192 (8 KB).

What you are doing

You are setting the size of the buffer for writig repsonse body. It has nothing to do with the response headers. This is mainly used for flushing responses once the buffer size is reached.

What you need to do

You need to set the maxHttpHeaderSize property in Tomcat Configuration

You need to modify the HTTP connector's maxHttpHeaderSize in server.xml, not the code.

See here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM