Spring Boot File文件
Spring Boot File文件
介绍
在Spring Boot中,处理文件上传可以通过以下步骤完成:
配置文件上传属性:打开
application.properties
或application.yml
配置文件,并添加如下属性:# 设置最大文件大小(默认为1MB) spring.servlet.multipart.max-file-size=10MB # 设置请求的最大大小(默认为10MB) spring.servlet.multipart.max-request-size=10MB
这些属性用于配置文件上传的限制,你可以根据需要进行调整。
创建文件上传接口:在你的控制器类中创建一个处理文件上传的接口。可以使用
@PostMapping
注解和@RequestParam
注解来接收文件参数。示例代码如下:@RestController public class FileUploadController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { // 处理文件上传逻辑 // ... return "File uploaded successfully!"; } }
在上面的示例中,
@RequestParam("file")
用于将上传的文件绑定到MultipartFile
对象。配置文件上传存储路径:如果你希望将上传的文件保存到服务器的特定目录中,可以在配置文件中添加以下属性:
# 设置文件上传的存储路径 spring.servlet.multipart.location=/path/to/upload/folder
确保将
/path/to/upload/folder
替换为你想要保存文件的实际路径。处理文件上传逻辑:在文件上传接口中,你可以编写逻辑来处理上传的文件。例如,你可以通过调用
file.getInputStream()
获取文件的输入流,并将其保存到指定的路径中。示例代码如下:@RestController public class FileUploadController { @Value("${spring.servlet.multipart.location}") private String uploadLocation; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { try { // 获取文件名 String fileName = file.getOriginalFilename(); // 构建保存文件的路径 String filePath = uploadLocation + "/" + fileName; // 保存文件 file.transferTo(new File(filePath)); return "File uploaded successfully!"; } catch (IOException e) { // 处理异常 return "File upload failed!"; } } }
在上述示例中,通过
file.getOriginalFilename()
获取上传文件的原始文件名,然后使用file.transferTo()
方法将文件保存到指定路径。测试文件上传:使用任何HTTP客户端(如Postman)发送
multipart/form-data
格式的POST请求到/upload
接口,并携带一个名为file
的文件参数。确保文件参数的键名与@RequestParam
注解中指定的参数名相匹配。
Demo
项目结构
源码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class A07SpringBootFileApplication {
public static void main(String[] args) {
SpringApplication.run(A07SpringBootFileApplication.class, args);
}
}
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@RestController
public class FileUploadController {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req) {
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
System.out.println(realPath);
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
uploadFile.transferTo(new File(folder, newName));
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
return filePath;
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败!";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload File</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="请选择文件" multiple>
<input type="submit" value="上传">
</form>
</body>
</html>
server.port=8081