?Ioutil 是hutool包
@GetMapping(value = "/down/file")
public void downloadFileStream(HttpServletResponse response) throws UnsupportedEncodingException {
String name = "公共人员信息模板.xlsx";
String fileUrl = bootdoConfig.getUploadPath() + name;
File file = FileUtil.file(fileUrl);
if (StringUtils.isEmpty(fileUrl) || file == null || !file.exists()) {
System.out.println("文件不存在");
}
ServletOutputStream outputStream = null;
response.setContentType("application/force-download");// 设置强制下载不打开
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
try {
outputStream = response.getOutputStream();
outputStream.write(FileUtil.readBytes(file));
} catch (Exception e) {
e.printStackTrace();
}
IoUtil.close(outputStream);
}
|