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() + "\"");