pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<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>
</dependencies>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vivi</title>
</head>
<body>
<h1>hello Vivi</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
FileUploadController.java
import org.springframework.web.multipart.MultipartFile;
@PostMapping("upload")
@ResponseBody
public void upload(MultipartFile file, HttpServletRequest request) throws IOException {
System.out.println(file.getOriginalFilename());
System.out.println(file.getContentType());
System.out.println(file.getSize());
System.out.println(file.getInputStream());
String realPath = request.getServletContext().getRealPath("/files");
System.out.println(realPath);
File newFile = new File(realPath);
if (!newFile.exists()) newFile.mkdirs();
file.transferTo(new File(newFile, file.getOriginalFilename()));
}
文件上传优化
@PostMapping("upload")
@ResponseBody
public void upload(MultipartFile file) throws IOException {
String realPath = ResourceUtils.getURL("classpath:").getPath() + "/static/files";
System.out.println(realPath);
File newFile = new File(realPath);
if (!newFile.exists()) newFile.mkdirs();
file.transferTo(new File(newFile, file.getOriginalFilename()));
}
动态修改上传文件的位置(最终)
application.properties
# 用来指定服务器端文件大小的限制
spring.servlet.multipart.max-file-size=300MB
# 用来指定客户端文件大小的限制
spring.servlet.multipart.max-request-size=300MB
fileLocation=static/files
FileUploadController.java
@Value("${fileLocation}")
private String fileLocation;
@PostMapping("upload")
@ResponseBody
public void upload(MultipartFile file) throws IOException {
String realPath = ResourceUtils.getURL("classpath:").getPath() + fileLocation;
File newFile = new File(realPath);
if (!newFile.exists()) newFile.mkdirs();
String fileName = date.getTime() +"@" + file.getOriginalFilename();
file.transferTo(new File(newFile, fileName));
}
文件下载
index.html
<h1>文件下载</h1>
<a href="/download?fileName=axios.png">axios.png</a><br>
<a href="/download?fileName=layui.png">layui.png</a>
@Value("${fileLocation}")
private String fileLocation;
@GetMapping("download")
public void download(String fileName, HttpServletResponse response) throws IOException {
String realPath = ResourceUtils.getURL("classpath:").getPath() + fileLocation;
FileInputStream inputStream = new FileInputStream(new File(realPath, fileName));
response.setHeader("content-disposition", "attachment; fileName=" + fileName);
ServletOutputStream outputStream = response.getOutputStream();
int len = 0;
byte[] data = new byte[1024];
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
outputStream.close();
inputStream.close();
}