第二十四讲 SpringBoot下载文件
1.pom.xml 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 前端:fileUpload.html
<html>
<head>
<meta charset="UTF-8"/>
<title>文件下载示例</title>
</head>
<body>
<h2>文件下载示例</h2>
<hr/>
<a href="/downloadFile">下载</a>
</body>
</html>
3. 后端:DownloadController.java
package com.springboot.download.controller;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
@Controller
@RequestMapping("/")
public class DownloadController {
@RequestMapping(value = "/download", method = RequestMethod.GET)
public String Download() {
return "fileDownload";
}
@RequestMapping("/downloadFile")
public void downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = "SpringBoot文件下载测试.txt";
String path = "D:\\";
File file = new File(path, fileName);
if (file.exists()) {
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("text/plain");
response.setContentType("appliaction/force-download");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
OutputStream outputStream = response.getOutputStream();
byte[] buff = new byte[1024];
int i = bufferedInputStream.read(buff);
while (i != -1) {
outputStream.write(buff, 0, i);
outputStream.flush();
i = bufferedInputStream.read(buff);
}
System.out.println("下载成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
参考:SpringBoot下文件上传与下载的实现