文件下载

指定超链接

 <a href="/user/download">点击下载图片</a>

代码如下

 1     @RequestMapping("download")
 2     public ResponseEntity<byte[]> download() throws IOException {
 3         //定义一个文件
 4         File file = new File("F:\picture\sss.jpg");
 5         //设置响应头
 6         HttpHeaders httpHeaders = new HttpHeaders();
 7         //相应类型,text,json.jpg,png等等文件类型
 8         httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
 9         // 指定附件名称
10         httpHeaders.setContentDispositionFormData("attachment",file.getName());
11         //返回相应实体   HttpStatus.OK是状态码
12         return  new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
13     }

文件下载结束!!

原文地址:https://www.cnblogs.com/ych961107/p/11872045.html