一切工作正常,但只有当文件很小,大约1MB,当我用更大的文件,像20MB我的浏览器显示它,而不是强制下载,我尝试了很多标题到目前为止,现在我的代码看起来:如何强制浏览器下载文件?
PrintWriter out = response.getWriter();
String fileName = request.getParameter("filename");
File f= new File(fileName);
InputStream in = new FileInputStream(f);
BufferedInputStream bin = new BufferedInputStream(in);
DataInputStream din = new DataInputStream(bin);
while(din.available() > 0){
out.print(din.readLine());
out.print("\n");
}
response.setContentType("application/force-download");
response.setContentLength((int)f.length());
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition","attachment; filename=\"" + "xxx\"");//fileName);
in.close();
bin.close();
din.close();
设置内容类型应该这样做。它是哪个浏览器? –
哪个浏览器? Octet-stream应该是正确的答案,但IE的旧版本用于尝试和“嗅探”内容类型,而不管Web服务器告诉它什么 – Paolo
chrome,ff 3.6,以及其他可能... – dieeying