gpt4 book ai didi

java - 在 Internet Explorer 中使用 ServletOutputStream 通过 HTTPS 从 Servlet 返回 CSV 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:43 31 4
gpt4 key购买 nike

我有一个 Servlet,它返回一个 csv 文件,该文件在 Internet Explorer 和 Firefox 中都通过 HTTP“工作”。当我通过 HTTPS 执行同一个 Servlet 时,只有 firefox 继续通过 HTTPS 下载 csv 文件。我认为这不一定是描述的 Internet 6 或 7 问题 on MSDN :

消息是:

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

请注意,此消息后该站点仍处于“运行状态”,您可以继续浏览该站点,只是下载提示此消息的 CSV。我已经能够从其他 j2ee 应用程序通过 IE 上的 https 访问类似文件,所以我相信这是我们的代码。 我们不应该关闭 bufferedOutputStream 吗?

更新

是否关闭输出流:我在 java posse 论坛和 discussion 上问过这个问题还有有见地的。最后,似乎没有容器应该依赖“客户端”(在这种情况下是您的 servlet 代码)来关闭此输出流。因此,如果您未能关闭 servlet 中的流而导致出现问题,那么这更多地反射(reflect)了您的 servlet 容器实现不当,而不是您的代码。我确定了来自 Sun、Oracle 和 BEA 的 IDE 和教程的行为,以及它们在是否关闭流方面也不一致。

关于 IE 的特定行为:在我们的案例中,一个单独的产品“Oracle Web 缓存”引入了额外的 header 值,这仅由于 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();
}
}
}

最佳答案

我真的很困惑你的“从背部通过胸部进入头部”的写入机制。为什么不简单(servlet 输出流将是 bufferend,那是容器的东西):

byte[] csv = dao.getCSV();
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=data.csv"));
reponse.setContentLength(csv.length);
ServletOutputStream out = response.getOutputStream();
out.write(csv);

也不需要刷新输出流或关闭。

header 内容不应被 IE 解析为区分大小写,但谁知道呢:不要驼峰式 fileName。下一个问题是编码。 CSV 是文本,因此您应该改用 getWriter() 或 getOutputStream() 并将内容类型设置为“text/csv; charset=UTF-8”,例如.但是 dao 应该提供 CSV 作为字符串而不是 byte[]。

servlet 代码与 HTTPS 无关,因此协议(protocol)与服务器端无关。我希望您可以使用 HTTP 从本地主机测试 servlet。

您的应用程序中的过滤器怎么样?例如,过滤器也可以使用缓存控制设置 HTTP header (或作为页脚)。

关于java - 在 Internet Explorer 中使用 ServletOutputStream 通过 HTTPS 从 Servlet 返回 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/899858/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com