public void downloadDeviceTemp(HttpServletRequest request, HttpServletResponse response) { try { String realPath = request.getSession().getServletContext().getRealPath("/page"); //读到流中 InputStream stream = new FileInputStream(realPath + "/examine/excel/devicetemp/device_keypoint_temp.xlsx"); //重置 清除首部的空白行 response.reset(); //不存在的文件类型,出现下载提示 response.setContentType("bin"); String fileName = new String("重点点位设备导入模板.xlsx".getBytes("gb2312"), "ISO8859-1"); response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """); byte[] b = new byte[100]; int len; while ((len = stream.read(b)) > 0) { response.getOutputStream().write(b, 0, len); } stream.close(); } catch (IOException e) { e.printStackTrace(); } }