模拟通过 http 下载本地 Excel 数据文件:
/**
* 模拟下载本地Excel文件
* @param response
* @throws IOException
*/
@GetMapping("/downloadLocal")
public void downloadFromLocalFile(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String exportFileName = URLEncoder.encode("导出数据文件", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + exportFileName + ".xlsx");
String fileName = "/Users/xiaochen" + File.separator + "Downloads" + File.separator + "K2KbendiTest.xlsx";
File file = new File(fileName);
if (file.exists()) {
byte[] b = new byte[100];
int len;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
while ((len = inputStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}