当前位置 : 主页 > 编程语言 > java >

SpringMVC下载Excel

来源:互联网 收集:自由互联 发布时间:2021-06-28
SpringMVC下载Excel @RequestMapping("xls")@ResponseBodypublic String xls(HttpServletResponse response) {response.setCharacterEncoding("UTF-8");response.setContentType("application/x-xls");response.setHeader("Content-Disposition", "attachme
SpringMVC下载Excel
@RequestMapping("xls")
@ResponseBody
public String xls(HttpServletResponse response) {
	response.setCharacterEncoding("UTF-8");
	response.setContentType("application/x-xls");
	response.setHeader("Content-Disposition", "attachment; filename=" + "download-file.xlsx");
	InputStream inputStream = null;
	OutputStream outStream = null;
	try {
		inputStream = new FileInputStream("D:\\xls\\sample.xlsx");
		outStream = response.getOutputStream();
		byte[] buffer = new byte[512];
		int numberRead = 0;
		while ((numberRead = inputStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, numberRead);
		}
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}
上一篇:断点续传JAVA实现
下一篇:日志类
网友评论