使用Struts2下载文件并提示

使用Struts2下载文件并提示

问题描述:

我正在使用Struts2下载文件。当我点击按钮下载文件时,文件会自行下载并在Excel中打开,而不是我希望它下载,当我右键点击它时,我应该可以打开或保存它。在IEFirefox我得到一个窗口来打开或保存文件,但在Chrome文件文件本身打开在Excel中。有没有办法把它在Chrome就像FirefoxIE使用Struts2下载文件并提示

行动

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标头设置为attachment(您已设置)。相反,inline会尝试用浏览器插件打开文件,而不询问任何内容。

这就是说,你的问题背后的原因是,你(或某人访问您的计算机)已事先通知Chrome浏览器这种类型总是打开的文件,这就是为什么它不问你了约该怎么做:

enter image description here


的解决方案是清除此选项从设置


自定义和控制谷歌浏览器设置+显示高级设置清除自动打开设置


enter image description here

+1

你是真棒安德烈。你明白我的问题,没有任何进一步的调查,你回答了我正在寻找的东西。我被告知这是一个浏览器行为,但没有人知道如何解决它。再次感谢你。 – Jaykumar