HeadersTooLargeException - 响应标头

问题描述 投票:0回答:2

我在 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() + "\"");
java spring-mvc tomcat download http-headers
2个回答
3
投票

我也有类似的问题。就我而言,响应对象确实有太多标头,并且它们的总长度超过了

maxHttpHeaderSize
。尝试使用调试器检查您的响应对象:在 Tomcat 上,它可能包含一个具有
coyoteResponse
字段的
headers
对象。也许您会发现一些无关的 Set-Cookie 标头或类似内容?


0
投票

我在 spring-boot 项目中遇到了类似的问题,只有在 application.yml 文件中增加以下属性后才能解决我的问题:

server:
  tomcat:
    max-http-response-header-size: 24KB # default 8KB

显然在tomcat中响应和请求标头可以单独设置 春季启动:3.2.2

© www.soinside.com 2019 - 2024. All rights reserved.