1.文件名template.xlsx
2.位置是resourse/download/template.xlsx
3.前台a标签进行链接
4.下载方式有很多种,我们采用springmvc提供的一种
5.文件可以是图片,excel,word,等等
6.引入依赖jar包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
7.源代码:
@RequestMapping("/downloadTemplatex")
public ResponseEntity<byte[]> downloadTemplatex() throws Exception {
File path = ResourceUtils.getFile("classpath:download/template.xlsx");
HttpHeaders headers = new HttpHeaders();
String filename = new String(path.getName().getBytes("utf-8"), "iso-8859-1");
headers.setContentDispositionFormData("attachment", filename);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(FileUtils.readFileToByteArray(path), headers, HttpStatus.CREATED);
return responseEntity;
}
7.参考于下面: