前台用a标签访问,不要用ajax,不然没反应
@RequestMapping(value="/download",method = RequestMethod.GET, produces = "text/html;charset=utf-8") public void download( @RequestParam(value="path",required=false) String path, @RequestParam(value="fileName",required=false) String fileName, HttpServletRequest req, HttpServletResponse resp ) { try { req.setCharacterEncoding("UTF-8"); //第一步:设置响应类型 resp.setContentType("application/force-download");//应用程序强制下载 //第二读取文件 // String path = getServletContext().getRealPath("/up/"+name); InputStream in = new FileInputStream(path); //设置响应头,对文件进行url编码 fileName = URLEncoder.encode(fileName, "UTF-8"); resp.setHeader("Content-Disposition", "attachment;filename="+fileName); resp.setContentLength(in.available()); //第三步:老套路,开始copy OutputStream out = resp.getOutputStream(); byte[] b = new byte[1024]; int len = 0; while((len = in.read(b))!=-1){ out.write(b, 0, len); } out.flush(); out.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } }