我正在尝试创建一个页面,用户可以在其中下载一些 .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 字节的文件......