一切工作正常,但只有當文件很小,大約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