【发布时间】:2009-05-22 20:32:13
【问题描述】:
我有一个 Servlet,它返回一个 csv 文件,该文件在 Internet Explorer 和 Firefox 中都通过 HTTP “工作”。当我通过 HTTPS 执行相同的 Servlet 时,只有 firefox 继续通过 HTTPS 下载 csv 文件。我认为这不一定是on MSDN 描述的 Internet 6 或 7 问题:
消息是:
Internet Explorer 无法下载 来自 mydomain.com 互联网的 data.csv 资源管理器无法打开此 互联网网站。请求的站点是 要么不可用,要么找不到。 请稍后再试。
请注意,在收到此消息后,该站点仍处于“启动”状态,您可以继续浏览该站点,只是下载了提示此消息的 CSV。我已经能够通过 IE 上的 https 从其他 j2ee 应用程序访问类似的文件,所以我相信这是我们的代码。 我们不应该关闭 bufferedOutputStream 吗?
更新
是否关闭输出流: 我在 java posse 论坛上问了这个问题,discussion 也很有见地。最后,似乎没有容器应该依赖“客户端”(在这种情况下是您的 servlet 代码)来关闭此输出流。因此,如果您未能关闭 servlet 中的流导致出现问题,则它更多地反映了 servlet 容器的不良实现而不是您的代码。我定位了来自 Sun、Oracle 和 BEA 的 IDE 和教程的行为,以及它们在是否关闭流方面也不一致。
关于 IE 特定行为:在我们的案例中,一个单独的产品“Oracle Web 缓存”引入了额外的标头值,这仅因为 IE 实现“无缓存”要求的方式而影响 Internet Explorer( see the MSDN article)。 代码是:
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
ServletOutputStream out = null;
ByteArrayInputStream byteArrayInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
response.setContentType("text/csv");
String disposition = "attachment; fileName=data.csv";
response.setHeader("Content-Disposition", disposition);
out = response.getOutputStream();
byte[] blobData = dao.getCSV();
//setup the input as the blob to write out to the client
byteArrayInputStream = new ByteArrayInputStream(blobData);
bufferedOutputStream = new BufferedOutputStream(out);
int length = blobData.length;
response.setContentLength(length);
//byte[] buff = new byte[length];
byte[] buff = new byte[(1024 * 1024) * 2];
//now lets shove the data down
int bytesRead;
// Simple read/write loop.
while (-1 !=
(bytesRead = byteArrayInputStream.read(buff, 0, buff.length))) {
bufferedOutputStream.write(buff, 0, bytesRead);
}
out.flush();
out.close();
} catch (Exception e) {
System.err.println(e); throw e;
} finally {
if (out != null)
out.close();
if (byteArrayInputStream != null) {
byteArrayInputStream.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
}
}
【问题讨论】:
-
Firefox 有效。你能描述一下IE会发生什么吗?你没有得到任何东西,它是否挂起,文件是否被截断,...?
-
MSDN 文章突出显示了该消息。我会尽快用消息更新问题。
标签: java servlets jakarta-ee