问题
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';");
回答1:
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(){ ... }
回答2:
The error means that the header field is set twice; you should be able to see that in an HTTP trace. Thus you need to find out why it's set twice.
来源:https://stackoverflow.com/questions/27836375/duplicate-headers-received-from-server-using-struts2