版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37599827/article/details/89138903
下面的例子是java springboot项目从服务器下载excel模板
前端
<a href="/serology/downloadExcel" >下载导入excel模板</a>
后台
/**
* 描述:下载外部案件导入模板
* @param response
*/
@RequestMapping("/downloadExcel")
@ResponseBody
public void downloadExcel(HttpServletResponse response) {
try {
String fileName = "excel.xls"; // 文件名称
//获取文件的路径
String rootpath = this.getClass().getClassLoader().getResource(".").getPath();
String filePath = rootpath + "/templates/" + fileName;
// 读到流中
InputStream inStream = new FileInputStream(filePath);//文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("血清学筛查数据模板.xls", "UTF-8"));
// 循环取出流中的数据
byte[] b = new byte[200];
int len;
while ((len = inStream.read(b)) > 0){
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}