gpt4 book ai didi

java - IE8 要求在 .xls 文件上打开/保存两次

转载 作者:行者123 更新时间:2023-12-01 15:18:27 26 4
gpt4 key购买 nike

我正在通过 Servlet 生成 Excel 文档。当我将响应发送回客户端 (IE8) 时,会弹出“打开/保存”对话框,但要求用户在采取操作之前单击两次选项。这在 Firefox 中不会发生。我不知道为什么会发生这种情况。下面是创建适当流的相关代码。

结果包含 Excel XML。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=TestFile.xls");

InputStream in = new ByteArrayInputStream(result.toString().getBytes("UTF-8"));
ServletOutputStream out = response.getOutputStream();

try
{
byte[] outputByte = new byte[4096];

while(in.read(outputByte, 0, 4096) != -1)
out.write(outputByte, 0, 4096);
}
finally
{
in.close();
out.flush();
out.close();
}

编辑我注意到在单击选项之前等待 5 秒以上效果很好。当立即点击一个选项时,似乎只询问两次。

最佳答案

此代码适用于我的应用程序中的每种类型的文件

  InputStream in = blob.getBinaryStream();
// Output the blob to the HttpServletResponse

String codedfilename = "";
//this code resolves the issue with the encoding of the downloaded filename
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE"))
{
codedfilename = URLEncoder.encode(/*here goes the filename*/, "UTF8");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename=" + codedfilename);
}
else if (null != agent && -1 != agent.indexOf("Mozilla"))
{
response.setCharacterEncoding("UTF-8");
//It does not seem to make a difference whether Q or B is chosen
codedfilename = MimeUtility.encodeText(rset.getString("FILE_NAME"), "UTF8", "B");
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment; filename=\"" + codedfilename + "\"");
}

BufferedOutputStream out =
new BufferedOutputStream(response.getOutputStream());
byte by[] = new byte[32768];
int index = in.read(by, 0, 32768);
while (index != -1) {
out.write(by, 0, index);
index = in.read(by, 0, 32768);
}
out.flush();

尝试一下并告诉我们

关于java - IE8 要求在 .xls 文件上打开/保存两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311507/

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