前端代码:
<h3>文件下载</h3> -->
<a
href="download?filename=${requestScope.user.headimage.originalFilename}">
用户头像:${requestScope.user.headimage.originalFilename}
</a>
注:需要使用href方式,不然会有报错。
后端代码:
public ResponseEntity<byte[]> download(String fileName, File file) throws IOException {
String dfileName = new String(fileName.getBytes(“gb2312”), “iso8859-1”);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData(“attachment”, dfileName);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
注:谷歌浏览器可能会报一个限制流的错(安全机制)。
response.addHeader(“Content-Length”,String.valueOf(body.length));