【问题标题】:HeadersTooLargeException - response headersHeadersTooLargeException - 响应标头
【发布时间】:2017-01-05 07:08:56
【问题描述】:

我已经在我的 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


【解决方案1】:

我遇到了类似的问题。在我的例子中,响应对象确实有太多的标头并且它们的总长度超过了maxHttpHeaderSize。尝试使用调试器检查您的响应对象:在 Tomcat 上,它可能包含一个具有 headers 字段的 coyoteResponse 对象。也许你会发现一些无关的 Set-Cookie 标头之类的?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 2017-03-05
    • 2011-12-14
    • 2015-11-12
    • 2018-05-06
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多