我想創建一個頁面,用戶可以下載一些.log文件。這是代碼:從模型和視圖下載文件在春天
if(action.equalsIgnoreCase("download")){
String file = (String)request.getParameter("file");
response.setHeader("Content-Disposition",
"attachment;filename="+file+"");
response.setContentType("text/plain");
File down_file = new File("log/"+file);
FileInputStream fileIn = new FileInputStream(down_file);
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();
return null;
}
我在哪裏做錯了? 當我下載按鈕單擊它正確地問我要保存文件,但它始終是一個0字節的文件...
'down_file.canRead()'返回true嗎? – Xaerxess 2012-08-01 16:01:35
是的,它返回true – Medioman92 2012-08-01 16:09:12