在Spring Boot中上传文件到服务器可以通过以下步骤实现:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
@RestController
public class FileUploadController {
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
// 处理文件上传逻辑
return "File uploaded successfully";
}
}
# 文件上传路径
spring.servlet.multipart.location=your_file_upload_directory
# 文件最大大小
spring.servlet.multipart.max-file-size=10MB
# 请求最大大小
spring.servlet.multipart.max-request-size=10MB
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Upload</title>
</head>
<body>
<h2>File Upload</h2>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file" required/>
<br/><br/>
<input type="submit" value="Upload"/>
</form>
</body>
</html>
注意:在上述代码中,your_file_upload_directory
需要替换为实际的文件上传路径。另外,还可以根据具体需求对文件上传逻辑进行进一步的处理,例如保存文件、返回上传成功的消息等。