downloadstreamstruts2

Struts2 link to open file only works with IE


I have a link on my page to open a PDF file which works fine with IE11 but Firefox gives me a "Corrupted Content Error" and Chrome gives me a "Duplicate headers received from server" error. The jsp for the link is

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

The displayDocument method is (I think the four lines before the catch are the ones which matter)

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

The struts commentAction code is

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

On IE11 the code asks if you want to save or open the file and opens the file in Adobe Reader if you choose open. Firefox and Chrome give the errors I mentioned at the beginning. If I remove

    response.setHeader("Content-Disposition", "attachment");

The errors go away but it opens in a new tab instead of Adobe Reader. Any help would be appreciated!


Solution

  • You should set the header via the result's parameter. It's using the same header attribute.

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

    Detailed information about stream result.