文件下载其实是触发了一个事件,跳转到另一界面
// 接受前台发送的下载请求路径和文件名
String url = request.getParameter("url");
System.out.println(url);
response.setContentType("application/x-download");// 设置为下载application/x-download
String[] p = url.split("/");
for(String s:p){
System.out.println(s);
}
String filedisplay = url.split("/")[0];// 下载文件时显示的文件保存名称
response.addHeader("Content-Disposition", "attachment;filename="
+ filedisplay);
url = request.getServletContext().getRealPath("/") + url;
File f = new File(url);
if (f.exists()) {
ServletOutputStream out = response.getOutputStream();
FileUtils.copyFile(f, out);
out.close();
}
在另一界面中(servlet),添加如上代码,即可获得下载功能