我在 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;
}
文件类存储来自数据库的文件信息:
public class Files {
private int id;
private String filename;
private String type;
private byte[] file;
}
我也尝试删除以下行,但仍然给出相同的错误:
response.setHeader("Content-Disposition",
"attachment; filename=\""+ file.getFilename() + "\"");