Java 获取文件下载以使用richfaces和IE8
正如在中一样,我在Firefox、Chrome、IE9等浏览器中使用了该页面 然而,IE8不起作用。我得到一个错误弹出窗口: Internet Explorer无法从[server]下载[filename].jsp Internet Explorer无法打开此Internet站点。请求的站点不可用或找不到。请稍后再试 我的代码如下:Java 获取文件下载以使用richfaces和IE8,java,jsf,internet-explorer-8,richfaces,ie8-compatibility-mode,Java,Jsf,Internet Explorer 8,Richfaces,Ie8 Compatibility Mode,正如在中一样,我在Firefox、Chrome、IE9等浏览器中使用了该页面 然而,IE8不起作用。我得到一个错误弹出窗口: Internet Explorer无法从[server]下载[filename].jsp Internet Explorer无法打开此Internet站点。请求的站点不可用或找不到。请稍后再试 我的代码如下: public String downloadFile() { // called from a h:commandLink String filen
public String downloadFile() { // called from a h:commandLink
String filename = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("file");
File file = new File(filename);
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
writeOutContent(response, file);
FacesContext.getCurrentInstance().responseComplete();
return "REFRESH";
}
private void writeOutContent(final HttpServletResponse res, final File content) {
if (content == null) {
return;
}
try {
res.setHeader("Pragma", "no-cache");
res.setDateHeader("Expires", 0);
res.setHeader("Content-Disposition", "attachment; filename=\"" + content.getName() + "\"");
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String contentType = servletContext.getMimeType(content.getName());
if (contentType == null) {
contentType = "application/octet-stream";
}
res.setContentType(contentType);
FileInputStream fis = new FileInputStream(content);
ServletOutputStream os = res.getOutputStream();
int length = 0;
int data = fis.read();
while (data != -1) {
length += 1;
os.write(data);
data = fis.read();
}
fis.close();
res.setContentLength(length);
os.flush();
os.close();
} catch (final IOException ex) {
Logger.getLogger(ApplicationController.class.getName()).log(Level.SEVERE, null, ex);
}
}
我能想到的唯一一件事是,我在将文件写入输出流后设置了响应内容长度,但正如我所说的,它在现代浏览器中工作得非常好
问题是,我们在IE8中也有一些无法消除的javascript错误。尽管有错误,页面的其余部分似乎仍能正常工作。错误是“SimpleToglePanelManager未定义”,以及2倍的“预期对象”。根据其设计
如果服务器正在使用安全套接字层(SSL),并且在响应消息中添加了以下一个或两个HTTP头,则会出现问题:
Pragma:没有缓存
缓存控制:无缓存,最大年龄=0,必须重新验证
删除res.setHeader(“Pragma”,“无缓存”)代码>-按预期工作
另请参见此图。可能的副本的确,很遗憾我以前没有找到它。