<
a
href
=
"
${pageContext.request.contextPath}
/file/download?finame=
${user.image}
"
>
下载头像
</
a
>
private
String
filePath
=
"E:/javaWeb课程/springMVC/WebContent/image"
;
/*
* 下载
*/
@
RequestMapping(
"/download"
)
public
ResponseEntity<
byte
[]> download(String
finame
)
throws
IOException{
//获取文件路径
File
file
=
new
File(
filePath
,
finame
);
//设置响应的头部信息
HttpHeaders
headers
=
new
HttpHeaders();
//设置下载的MIME类型
headers
.setContentDispositionFormData(
"attachment"
,
finame
);
//设置ContentType
headers
.setContentType(MediaType.
APPLICATION_OCTET_STREAM
);
//声明返回对象
byte
[]
bytes
= FileUtils.readFileToByteArray(
file
);
ResponseEntity<
byte
[]>
entry
=
new
ResponseEntity<
byte
[]>(
bytes
,
headers
, HttpStatus.
CREATED
);
return
entry
;
}