html 代码 js部分<br> window.location.href= this.Baseurl+'/plan/down?file='+filename;
spring boot 后台代码 @GetMapping("/down") public HttpServletResponse down(HttpServletResponse response, HttpServletRequest request,@RequestParam("file") String files ) { String fileName = files;// 设置文件名,根据业务需要替换成要下载的文件名 if (fileName != null) { //设置文件路径 String pahre=System.getProperty("user.dir")+ "\\report\\"; File file = new File(pahre , fileName); if (file.exists()) { response.setContentType("application/force-download");// 设置强制下载不打开 response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名 byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } return null; }
文件的默认位置在当前运行目录的report文件下