I have implmented file download in my project in Spring mvc and while downloading the file it gives me below error on tomcat 7 server:
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.
I have also tried increasing the header size using below code in server.xml
<Connector port="8080" maxHttpHeaderSize="65536" protocol="HTTP/1.1" ... />
However, this is also not working and I still get the above error.
Below is the controller code for file download:
@RequestMapping(value = "/admin/file/download", method = RequestMethod.GET)
public @ResponseBody ModelAndView download(HttpServletRequest request,
HttpServletResponse response) throws Exception {
int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
Files file = this.filesManager.find(id);
response.setContentType(file.getType());
response.setContentLength(file.getFile().length);
response.setHeader("Content-Disposition", "attachment; filename=\""+ file.getFilename() + "\"");
FileCopyUtils.copy(file.getFile(), response.getOutputStream());
response.flushBuffer();
return null;
}
The class Files stores the file information from database:
public class Files {
private int id;
private String filename;
private String type;
private byte[] file;
}
I have also tried removing the below line but still gives the same error:
response.setHeader("Content-Disposition",
"attachment; filename=\""+ file.getFilename() + "\"");
I had a similar problem in my spring-boot project, mine could only be solved once I increased the following property in the application.yml file:
server:
tomcat:
max-http-response-header-size: 24KB # default 8KB
apparently in tomcat response and request headers can be set individually spring boot: 3.2.2
I had a similar problem. In my case, the response object indeed had too many headers and their overall length exceeded maxHttpHeaderSize
. Try inspecting your response object using a debugger: being on Tomcat, it probably contains a coyoteResponse
object that has a headers
field. Maybe you'll find some extraneous Set-Cookie headers or such?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With