把文件放到webapp目录下
// 下载本地文件
String fileName = new String("xxxxx.xlsx"); // 文件的默认保存名
String webappRootPath = request.getServletContext().getRealPath("/");
// 读到流中
FileInputStream inStream = new FileInputStream(new File(webappRootPath+File.separator+"directory"+File.separator+"xxxxx.xlsx"));// 文件的存放路径
// 设置输出的格式
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}