================== Spring MVC下载文件 ================== 1 前端 /src/main/webapp/getBooks.jsp: ... ... ... ... 2 控制器 ... public class BookController { ... @RequestMapping("/getCover") public void getCover(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception { // 目录名 String dir = request.getServletContext().getRealPath("images"); // 存储路径 String filepath = dir + "/" + filename; // 不可识别的内容类型迫使浏览器保存而非直接打开 response.setContentType("application/exe"); // 浏览器保存文件名 response.addHeader("Content-Disposition", "attachment;filename=" + filename); // 文件内容 IOUtils.copy(new FileInputStream(filepath), response.getOutputStream()); } ... } 例程:SpringFile