第一步:创建SpringBoot项目
Maven依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
com.fasterxml.jackson.core
jackson-databind
application.yml
spring:
#前缀,也就是模板存放的路径
thymeleaf:
prefix: classpath:/templates/
#编码格式
encoding: UTF-8
#关闭缓存,不然无法看到实时页面
cache: false
#后缀
suffix: .html
#设置不严格的html
mode: HTML
servlet:
content-type: text/html
在resources/static目录下添加一个名字为obj.jpg的图片。
第二步:编写前台页面:
文件下载
方式一:下载文件
文件
方式三:文件
第三步:编写后台Controller代码:
@Controller
public class FileDownloadController {
@GetMapping("/download1")
public ResponseEntity 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(value = "/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("/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;
}
}