导入依赖
<!-- 添加web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Thymeleaf模板起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.2</version>
</dependency>
yml配置文件配置的被下载文件的路径
spring:
upload-path: F:/file/upload/
前端
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>下载文件</title>
</head>
<body>
<h2>下载文件1</h2>
<a th:href="@{/download1/1.png}">1.png</a>
<h2>下载文件2</h2><br>
<form th:action="@{/download2}" method="post">
下载文件:<input th:type="text" name="filename"><br>
<input th:type="submit" value="下载"><br>
</form>
<h2>下载文件3</h2>
<a th:href="@{/download3?filename=1.png}">1.png</a>
</body>
<h2>下载文件4</h2>
<a th:href="@{/download4?filename=1.png}">1.png</a>
</body>
</html>
后端-controller
package com.demo.controller;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Slf4j
@Controller
public class DownloadFileController {
@Value("${spring.upload-path}")
private String resourcePath;
@GetMapping("toDownload")
public String toDownload() {
return "download";
}
/**
* 使用 hutool 下载
* @param filename
* @param response
*/
@GetMapping("/download1/{filename}")
public void downloadfile1(@PathVariable("filename") String filename, HttpServletResponse response) {
String filepath = resourcePath + filename;
response.setCharacterEncoding("UTF-8");
ServletUtil.write(response,new File(filepath));
}
@PostMapping("/download2")
public void downloadFile2(@RequestParam("filename") String filename, HttpServletResponse response) {
String filepath = resourcePath + File.separator + filename;
log.debug("filepath is " + filepath);
//设置文件格式
response.reset();
response.setHeader("content-type", "image/png");
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
byte[] buff = new byte[1024];
//创建缓冲输入流
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(filepath));
int read = bis.read(buff);
while (read != -1) {
os.write(buff, 0, buff.length);
os.flush();
read = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@GetMapping("download3")
public ResponseEntity<InputStreamResource> downloadFile3(String filename) throws FileNotFoundException {
String filepath = resourcePath + filename;
InputStreamResource isr = new InputStreamResource(new FileInputStream(filepath));
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-disposition", "attachment; filename=" + filename)
.body(isr);
}
@GetMapping("/download4")
public ResponseEntity<ByteArrayResource> downloadFile4(@RequestParam("filename") String filename) throws IOException {
String filepath = resourcePath + filename;
byte[] bytes = FileUtils.readFileToByteArray(new File(filepath));
ByteArrayResource bar = new ByteArrayResource(bytes);
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-disposition", "attachment; filename=" + filename)
.body(bar);
}
}