HeadersTooLargeException - 响应标头 [英] HeadersTooLargeException - response headers

查看:93
本文介绍了HeadersTooLargeException - 响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring mvc 的项目中实现了文件下载,在下载文件时,它在 tomcat 7 服务器上出现以下错误:

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.

我还尝试使用 server.xml 中的以下代码增加标头大小

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;

}

类 Files 存储来自数据库的文件信息:

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

推荐答案

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

I had a similar problem. In my case, the response object indeed had too many headers and their overall length exceeded maxHttpHeaderSize. Try inspecting your response object using a debugger: being on Tomcat, it probably contains a coyoteResponse object that has a headers field. Maybe you'll find some extraneous Set-Cookie headers or such?

这篇关于HeadersTooLargeException - 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆