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

标签 java jsp download struts2 stream

我的页面上有一个打开 PDF 文件的链接,该文件在 IE11 下运行良好,但 Firefox 给我一个“内容损坏错误”,而 Chrome 给我一个“从以下位置收到的重复 header ”服务器” 错误。链接的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 上,代码会询问您是否要保存或打开文件,如果您选择打开,则在 Adob​​e Reader 中打开文件。 Firefox 和 Chrome 会出现我在开头提到的错误。如果我删除

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

错误消失,但它在新选项卡而不是 Adob​​e Reader 中打开。任何帮助将不胜感激!

最佳答案

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

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

有关stream result的详细信息.

关于java - Struts2 链接打开文件仅适用于 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639531/

相关文章:

java - 通过网络写入 4 个字节的无符号整数

下载特定的 Android 源代码

ios - 如何从URL确定文件的文件扩展名(Objective-C)

java - 与名单上的情侣一起工作,忽略顺序......一些优雅的方式?

java - MacOS 的 Eclipse/STS 中的包 View 样式出现问题

java - 如何重写扩展类的子方法

java - 将 jsp 页面移至 Angular 7

java - Spring 形式什么也不做

javascript - 如何在jsp文件中使用javascript添加请求参数

shell - 如何从网页抓取流媒体视频 list ?