搭建 springboot
项目
Maven
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
application.properties
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.emode=HTML
在 resources/static
目录下添加一个名字为 obj.jpg
的图片
前端页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>文件下载</title>
</head>
<body>
<a th:href="@{/download1?fileName=obj}">方式一:下载文件</a><br>
<a th:href="@{/download2?fileName=obj}"方式二:>文件</a><br>
<a th:href="@{/download3?fileName=application}">方式三:文件</a><br>
</body>
</html>
后台 Controller
@Controller
public class FileDownloadController {
@GetMapping(path = "/download1")
public ResponseEntity<InputStreamResource> downloadFile(String fileName) throws IOException {
String filePath = "E:/Demo/src/main/resources/static/" + fileName + ".jpg";
FileSystemResource fsr = new FileSystemResource(filePath);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fsr.getFilename()));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(fsr.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(fsr.getInputStream()));
}
@GetMapping(path = "/download2")
public void getDownload(@RequestParam("fileName") String fileName, HttpServletRequest request, HttpServletResponse response) {
String filePath = "E:/Demo/src/main/resources/static/" + fileName + ".jpg";
File downloadFile = new File(filePath);
ServletContext context = request.getServletContext();
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
mimeType = "application/octet-stream";
System.out.println("context getMimeType is null");
}
System.out.println("MIME type: " + mimeType);
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
downloadFile.getName());
response.setHeader(headerKey, headerValue);
try {
InputStream myStream = new FileInputStream(filePath);
IOUtils.copy(myStream, response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}
// 下载文件到用户桌面
@ResponseBody
@RequestMapping(path ="/download3")
public String downloadFile(String fileName, HttpServletResponse response) {
if (fileName != null) {
// 设置文件路径
String realPath = "E:/Demo/src/main/resources/";
fileName = fileName + ".yml";
File file = new File(realPath, fileName);
if (file.exists()) {
response.setContentType("application/octet-stream");//
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
}