代码
String downloadFilePath = "C:" + File.separator;// 被下载的文件在服务器中的路径,
String fileName = name+".xls";// 被下载文件的名称
Path file = Paths.get(downloadFilePath, fileName);
if (Files.exists(file)) {
response.setContentType("application/force-download");
// response.addHeader("Content-Disposition",
// "attachment; filename=" + fileName);
// 如果文件名有中文的话,进行URL编码,让中文正常显示
try {
response.addHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Files.copy(file, response.getOutputStream());
} catch (IOException ex) {
}
}