使用 Struts2 下载文件并提示

Download file using Struts2 with a prompt

我正在使用 Struts2 下载文件。当我单击按钮下载文件时,文件会自行下载并在 Excel 中打开,而不是我希望它下载,当我右键单击它时,我应该能够打开或保存它。在 IEFirefox 中,我得到一个 window 来打开或保存文件,但在 Chrome 中,文件文件在 Excel 中自行打开。有什么方法可以像 FirefoxIE

一样在 Chrome 中使用它

动作

public String viewReport() throws Exception {
    boolean returnReport;
    inputStream = new FileInputStream(DOCUSIGN_REPORT_FILE);

    try {
        returnReport = validateRequest();
        if (returnReport) {

        intgList = this.generateViewIntegrationReportData(getESignUIConfig());
        this.createCSVFile(intgList, DOCUSIGN_REPORT_FILE);

        } else {
            failureResponse(msgs, 400);
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        msgs.add(new Message(ESignatureIntegrationMessageTypeEnum.MESSAGE_TYPE_ERROR, 
                    UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_CODE_500, UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_TEXT_SERVICE_ERROR));
        failureResponse(msgs, 500);
        return null;
    }

    return UiIntegrationKeyConstants.REPORT_REPSONSE;
} 

Struts.xml

<action name="*Integration" method="{1}" class="foo.bar.ESignatureIntegrationAction">
    <result name="success" type="tiles">integrationView</result>  
    <result name="reportResponse" type="stream"> 
        <param name="contentType">application/text/csv</param>
        <param name="inputName">inputStream</param>  
        <param name="contentDisposition">
            attachment;filename="DocuSignEnvelopeReport.csv"
        </param> 
        <param name="bufferSize">4096</param>
    </result>  
</action>

询问是否使用桌面应用程序打开文件或保存文件的提示由 contentDisposition HTTP header 设置为 attachment(您已设置)给出。相反,inline 会尝试使用浏览器插件打开文件,而不询问任何内容。

也就是说,您的问题背后的原因是您(或有权访问您的计算机的人)之前指示 Chrome 始终打开此类文件 ,这就是为什么它不再询问您要做什么的原因:


解决方法是从设置清除这个选项


自定义和控制GoogleChrome设置+显示高级设置 清除 auto-opening 设置