Struts2 链接打开文件仅适用于 IE

问题描述 投票:0回答:1

我的页面上有一个打开 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方法是(我认为catch之前的四行是重要的)

    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;
}   

struts commentAction代码是

        <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 中打开。任何帮助将不胜感激!

java jsp download struts2 stream
1个回答
1
投票

您应该通过结果的参数设置标题。它使用相同的标头属性。

<param name="contentDisposition">attachment;filename="the.pdf"</param>

有关

stream
结果的详细信息

© www.soinside.com 2019 - 2024. All rights reserved.