我正在通过Servlet生成Excel文档。当我将响应发送回客户端(IE8)时,会弹出“打开/保存”对话框,但要求用户在执行操作前单击选择两次。这在Firefox中不会发生。我不知道为什么会这样。以下是创建适当流的相关代码。
result
包含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秒以上就可以了。它似乎只在立即点击选项时才会问两次。
答案 0 :(得分:1)
此代码适用于我的应用程序中的每种类型的文件
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();
尝试并让我们知道