SpringMVC下载Excel @RequestMapping("xls")@ResponseBodypublic String xls(HttpServletResponse response) {response.setCharacterEncoding("UTF-8");response.setContentType("application/x-xls");response.setHeader("Content-Disposition", "attachme
@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;
}
