SSM文件下载
- 获取下载文件位置所有信息
@GetMapping("/downLoadFiles")
public String downLoadFiles(HttpServletRequest request, Model model){
String realPath = request.getServletContext().getRealPath("/upload");
File savefile=new File(realPath);
List<String> fileNames = new ArrayList<>();
File[] files = savefile.listFiles();
for (File file:files) {
fileNames.add(file.getName());
}
model.addAttribute("fileNames",fileNames);
return "downLoad";
}
- 获取名字点击下载
@GetMapping("/fileDownLoad/{fileName}")
public ResponseEntity downLoad(@PathVariable("fileName") String fileName,HttpServletRequest request) throws IOException {
String realPath = request.getServletContext().getRealPath("/upload");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment",fileName);
return new ResponseEntity<>(
FileUtils.readFileToByteArray(new File(realPath,fileName))
,headers,
HttpStatus.OK);
}