第一步,本地文件配置地址,application.properties
fileUrl=d://ExcelFile//
第二步,前端控制器
//引入文件路径 @Value("${fileUrl}") private String fileUrl;
/** * 模板文件下载 * @param response */ @RequestMapping(value = "/loadxls",method = RequestMethod.GET) @ResponseBody public void loadxls(HttpServletResponse response) { InputStream fis = null; try { //引入文件存储地址 String filename = fileUrl+ "/设备信息数据登记表.xlsx"; System.out.println("下载的路径="+filename); //获得文件 File file = new File(filename); //获得字符输入缓冲流 fis = new BufferedInputStream(new FileInputStream(file)); //创建字符数组 byte[] buffer = null; buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); // 下载文件名 response.addHeader("Content-Disposition", "attachment;filename=" + new String("设备信息数据登记表.xlsx".getBytes("utf-8"), "iso8859-1")); response.addHeader("Content-Length", "" + file.length()); //创建输出流 OutputStream os = null; //创建输出字符缓冲流 os = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); //循环输出 os.write(buffer); os.flush(); os.close(); } catch (FileNotFoundException e) { e.getMessage(); } catch (IOException e) { e.getMessage(); } }