我的页面上有一个链接,可以打开一个PDF文件,该文件在IE11上工作正常,但Firefox给了我一个"Corrupted Content Error"
,Chrome给了我一个"Duplicate headers received from server"
错误。 链接的 jsp 是
<s:url var="documentLink" action="commentAction" method="displayDocument">
<s:param name="documentId"><s:property value="documentInfo.documentId"/></s:param>
</s:url>
<s:a target="_blank" href="%{documentLink}" tabindex="19"><s:text name="yes"/></s:a>
displayDocument 方法是(我认为捕获之前的四行是重要的)
public String displayDocument(){
String result = PDF;
try{
getDocumentManagerLocal();
DocumentInfo documentInfo = new DocumentInfo();
documentInfo.setDocument(documentManagerLocal.getDocumentByDocumentId(documentId));
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-Disposition", "attachment");
response.setContentLength(documentInfo.getDocument().length);
inputStream = new ByteArrayInputStream(documentInfo.getDocument());
} catch (Throwable e) {
result = ERRORS;
}
return result;
}
支柱注释操作代码是
<action name="commentAction" class = "gov.mo.dnr.rat.controller.comment.CommentAction">
<interceptor-ref name="authorizedUserStack">
<param name="fileUpload.maximumSize">31457280</param>
<param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref>
<result name="success" type="tiles">comment</result>
<result name="input" type="tiles">comment</result>
<result name="pdf" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDispostion">filename="the.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
在 IE11 上,代码会询问您是否要保存或打开文件,如果您选择打开,则会在 Adobe Reader 中打开文件。 Firefox和Chrome给出了我在开头提到的错误。 如果我删除
response.setHeader("Content-Disposition", "attachment");
错误消失了,但它会在新选项卡中打开,而不是Adobe Reader。 任何帮助将不胜感激!
您应该通过结果的参数设置标头。它使用相同的标头属性。
<param name="contentDisposition">attachment;filename="the.pdf"</param>
有关stream
结果的详细信息。