解决IE下通过HTTPS下载文件bug的代码(通过看SpringMVC源码碰到并记录下来)

在看SpringMVC源码时,在AbstractView类中看到一段有点疑惑的代码:

/**

 * Prepare the given response for rendering.

 * <p>The default implementation applies a workaround for an IE bug

 * when sending download content via HTTPS.

 * @param request current HTTP request

 * @param response current HTTP response

 */

protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {

                //文件下载时

if (generatesDownloadContent()) {

response.setHeader("Pragma", "private");

response.setHeader("Cache-Control", "private, must-revalidate");

}

}

为什么要加上蓝色标出的这两句呢?

其实方法上的注释已经说的比较明白了,是为了解决在ie下通过https下载文件的一个bug。为了详细了解这方面的知识点,特意搜索了相关的资料。

下面摘抄的是微软官方对该问题的说明和解决办法,链接地址:http://support.microsoft.com/default.aspx?scid=kb;en-us;316431


When you attempt to open or download a Microsoft Office document (.doc file, .xls file, .ppt file, and so on) from a secure Web site in Internet Explorer, you may receive one of the following error messages, even though the document is available and downloaded from the server: 


Error message 1


Internet Explorer cannot download file from server.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

Error message 2


The page cannot be displayed.

The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties, or you may need to adjust your 

browser settings.


Cannot find server or DNS Error

Error message 3


Office Application Name cannot open the file.

Error message 4


Publisher cannot find the file you specified.

The problem occurs if the server is using Secure Sockets Layer (SSL) and has added one or both of the following HTTP headers to the response 

message:

Pragma: no-cache Cache-control: no-cache,max-age=0,must-revalidate


出错原因:

In order for Internet Explorer to open documents in Office (or any out-of-process, ActiveX document server), Internet Explorer must save the file to 

the local cache directory and ask the associated application to load the file by using IPersistFile::Load. If the file is not stored to disk, this operation fails. 


When Internet Explorer communicates with a secure Web site through SSL, Internet Explorer enforces any no-cache request. If the header or headers are present, Internet Explorer does not cache the file. Consequently, Office cannot open the file.

解决办法:解决办法在这里,难怪Spring里要加上那两句,原来是为了防止出现no-cache header or headers

Web sites that want to allow this type of operation should remove the no-cache header or headers.


其他详细信息:

Files that are associated with Internet Explorer itself (including .txt files, .html files, .gif files, .jpg files, .xml files, and so on) do not generally have the problem. Files that are associated with non-Office applications may or may not have the problem, depending on the application. 

Web developers should note that some firewalls and security programs may add these headers automatically to all outbound HTTP responses. Even if 

you have not configured your Web server, Internet Server Application Programming Interface (ISAPI) extension, or Active Server Pages (ASP) script to return these headers, your site may include them by default. Check with your firewall or security administrator to determine if this is the case, and 

discuss the security risks to your company if you choose to disable this option to allow caching for these files.Steps to reproduce the behavior


In the Wwwroot folder for your IIS server, create a new ASP file. Name the file Testnocache.asp, and then add the following code to the file:

<%  Response.ContentType = "application/msword"  Response.AddHeader "Cache-control", "no-cache" %> <HTML><BODY>Test File Loaded in 

Microsoft Word</BODY></HTML> 


Configure your IIS server to use SSL.

On a client system that has Word installed, browse to the ASP file using HTTPS, as follows:

https://[servername]/testnocache.asp

You may be prompted to open or save the file on some systems. Regardless of which option you choose (or if you are not prompted to open or save 

the file), you receive an error message and are unable to open or save the file.


Remove the AddHeader line from the ASP file, and then browse to the URL from the client. The document opens in Word.


转载于:https://my.oschina.net/yck108/blog/423866

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值