modeFile/recordExcel.xlsx文件位于resources文件夹下。
@ResponseBody
@RequestMapping("recordExcel.ft")
public void getTotalExcel(FeverRegistrationVO vo, HttpSession session, HttpServletResponse response){
//SessionInfo sessionInfo = (SessionInfo)session.getAttribute("sessionInfo");
try {
//获取资源
ClassPathResource resource = new ClassPathResource("modeFile/recordExcel.xlsx");
//获取输入流
FileInputStream fileInputStream=new FileInputStream(resource.getFile());
//创建字节数组( inputStream.available()方法获取文件大小,FileInputStream中已经重写了这个方法,重写之后的方法是通过文件的描述来获取文件的大小的。)
byte[] buffer = new byte[fileInputStream.available()];
//转换为字节数组
fileInputStream.read(buffer);
//关闭流
fileInputStream.close();
//获取response对象的输出流
ServletOutputStream outputStream = response.getOutputStream();
//设置响应类型,contentType它定义服务器发送给客户端内容的MIME类型。
response.setContentType("application/msexcel");
//设置响应头(如果需要提示用户保存,就要利用Content-Disposition进行一下处理,关键在于一定要加上attachment)
response.addHeader("Content-Disposition", "attachment;filename="+new String("recordExcel.xlsx".getBytes("gb2312"),"ISO8859-1"));
//创建对应的字节缓冲输出流
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//写出缓冲的输出字节(write是写到缓冲区中,如果你需要使write马上写入到文件中,就需要调用flush)
toClient.write(buffer);
//刷新此输出流并强制写出所有缓冲的输出字节。
toClient.flush();
//关闭流
toClient.close();
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("获取设备报表异常:"+Log4jUtils.getTrace(e));
}
}
jsp页面处理
function reportTest(){
// $("#query_form_region").val(regionId);
$("#query_form04").attr('action','<%=basePath%>/store/recordExcel.ft');
$("#query_form04").submit();
}