Search code examples
javahttpstruts2http-headerscontent-disposition

duplicate headers received from server using Struts2


I am using Struts2 in an application. I need to download excel file(.xlsx and .xls formats). This is working properly in IE but in Chrome it is showing error

"Duplicate headers received from server"

I use quotes before the file name("<File Name"). Still it is not working in chrome. Below is the code snippets used in my application.

struts.xml

<action name="*Excel" method="{1}" class="ReportUtilityAction">
    <result name="success" type="stream">
        <param name="contentType">application/vnd.ms-excel</param>
        <param name="inputName">fileInputStream</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

I have mentioned the content-disposition in the action class as

static final private String Content = "Content-Disposition";

HttpServletResponse response = this.getHttpResponse();
response.setHeader(Content, "attachment;filename='Export.xlsx';");

Solution

  • You can set contentDisposition in the same way you've set the other headers: in struts configuration.

    <result name="success" type="stream">
        <param name="contentDisposition">attachment;filename="Export.xlsx";</param>
        <param name="contentType">application/vnd.ms-excel</param>
        <param name="inputName">fileInputStream</param>
        <param name="bufferSize">1024</param>
    </result>
    

    You can also have it parameterized by using the ${} notation, with a corresponding getter in the Action:

    <param name="contentDisposition">attachment;filename="${filename}";</param>
    
    public String getFilename(){ ... }