我已经在Spring mvc的项目中实现了文件下载,在下载文件时,它在tomcat 7服务器上给出了以下错误:
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.
我还尝试使用server.xml中的以下代码增加标头大小
<Connector port="8080" maxHttpHeaderSize="65536" protocol="HTTP/1.1" ... />
但是,这也无效,我仍然遇到上述错误。
以下是文件下载的控制器代码:
@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;
}
类Files存储来自数据库的文件信息:
public class Files {
private int id;
private String filename;
private String type;
private byte[] file;
}
我也尝试删除以下行,但仍然会出现同样的错误:
response.setHeader("Content-Disposition",
"attachment; filename=\""+ file.getFilename() + "\"");
答案 0 :(得分:1)
我有类似的问题。就我而言,响应对象确实有太多标题,其总长度超过maxHttpHeaderSize
。尝试使用调试器检查您的响应对象:在Tomcat上,它可能包含一个coyoteResponse
对象,其中包含headers
字段。也许您会找到一些无关的 Set-Cookie 标题或其他?