// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
filename = URLEncoder.encode(filename,"UTF-8");
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ filename);
response.addHeader("Content-Length", "" + file.length());
OutputStream stream = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
stream.write(buffer);
stream.flush();
fis.close();
stream.close();
注:只能以form方式提交浏览器才有下载提示,ajax提交不会有浏览器下载提示