controller层代码
@RequestMapping(value = "/download")
public void downloadExcel(HttpServletRequest request,HttpServletResponse response /*@RequestBody String path*/ ) throws IOException {
String path = "D:\\"; //这里获取的目录是写死的,也可以从前端动态获取目录,使用@requestBody注解获取, 上面我注解掉了。
String file_name ="xxxx.xlsx"; //excel文件名
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(file_name,"utf-8"));
//新建文件输入输出流
OutputStream os = null;
FileInputStream fs = null;
try{
File f = new File(path);//新建File对象
os = response.getOutputStream();//新建文件输入输出流对象