Java 使用IE的“另存为”对话框仅在writer关闭后打开
在Firefox中,当用户单击下载链接时,下载对话框会立即打开,但在编写器上调用close()之前,我很难让IE显示另存为对话框 我无法下载初始文件大小,因为它是动态生成的Java 使用IE的“另存为”对话框仅在writer关闭后打开,java,internet-explorer,servlets,download,Java,Internet Explorer,Servlets,Download,在Firefox中,当用户单击下载链接时,下载对话框会立即打开,但在编写器上调用close()之前,我很难让IE显示另存为对话框 我无法下载初始文件大小,因为它是动态生成的 ServletOutputStream os = null; Writer writer = null; try { os = response.getOutputStream(); // response is from an input param HttpServletResponse respons
ServletOutputStream os = null;
Writer writer = null;
try {
os = response.getOutputStream(); // response is from an input param HttpServletResponse
response.setHeader("Content-Disposition", "attachment");
response.setContentType("text/csv; charset=windows-1252");
writer = new OutputStreamWriter(os, Util.WINDOWS1252);
writer.write("\u0000"); // flush() will be effective only if there is something in the buffer, this works with firefox, download dialog opens, but with not with IE
writer.flush(); // forces to open the download popup instantly
fooManager.processCSV(some params..., writer); // generated on the fly and written in writer direclty
} catch (IOException e) {
logger.debug("csv: IOException ", e);
} finally {
if (writer != null) {
try {
writer.close(); // now IE will finally open download dialog...
} catch (IOException e) {
// ignore
}
}
}
谢谢 也许您需要文件名:
Content-Disposition: attachment; filename=FILENAME
要强制打开“另存为”对话框,请添加:
Content-Type: application/force-download
或:
我尝试了一个文件名,但它不工作。否则,如果没有文件名,则从下载URL获取文件名。您是对的。名称类似于最后一个斜杠后的url。根据浏览器的不同,这将尝试自动打开文件。在这种情况下,您应该发送一个通用的MIME来避免这种情况,就像Google Chrome中的mp3。1>在Fiddler中验证您的响应是否确实是流式的。2 >考虑使用伪MIME类型(例如应用程序/强制下载),以避免安装在机器上的任何Excel MIME过滤器的干扰。没有与IE一起工作。Chrome、Firefox与其他MIME类型一起工作良好。我尝试设置response.setContentLength(someRandomLength),IE立即打开下载。IE似乎需要一个文件大小来显示对话框,否则只有在下载文件后才会显示。问题是,csv是动态生成的,所以我不能有文件长度。(我正在运行Tomcat 7.0.42 btw)
Content-Type: application/octet-stream