@RequestMapping("/download/{dt}/{filename}")
@ResponseBody
public String download(@PathVariable("dt") String dt,@PathVariable("filename") String filename,HttpServletResponse response) throws IOException {
ApplicationHome appHome = new ApplicationHome(this.getClass());
String BASE_PATH = appHome.getDir().getParentFile().getParentFile().getAbsolutePath();
String uploadDir = BASE_PATH + "/src/main/resources/static/uploads/" + dt;
String fileName = uploadDir + "/" + filename + ".xlsx";
File f = new File(fileName);
FileInputStream fis = new FileInputStream(f);
InputStream is = new BufferedInputStream(fis);
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
response.reset();
response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
// 告知浏览器文件的大小
response.addHeader("Content-Length", "" + f.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
return fileName;
}