@GetMapping("/download/{fileName}")
@ResponseBody
public ResponseEntity<Object> downloadFile(@PathVariable(name = "fileName") String fileName) throws FileNotFoundException {
File file = new File(fileUploadRootDir + fileName);
if (!file.exists())
{
return new ResponseEntity<Object>("{\"code\": \"00002\", \"info\":\"File Not Found!\"}", HttpStatus.BAD_REQUEST);
}
InputStreamResource resource = new InputStreamResource( new FileInputStream( file ) );
HttpHeaders headers = new HttpHeaders();
headers.add ( "Content-Disposition",String.format("attachment;filename=\"%s",fileName));
headers.add ( "Cache-Control","no-cache,no-store,must-revalidate" );
headers.add ( "Pragma","no-cache" );
headers.add ( "
springboot 文件下载接口
最新推荐文章于 2024-10-07 16:01:13 发布
本文介绍了如何在SpringBoot应用中实现文件下载功能,重点在于使用fileUploadRootDir变量设置的文件存储路径来完成文件的读取和发送到客户端的过程。
摘要由CSDN通过智能技术生成