Controller层
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
@Controller
public class FileUploadController {
@PostMapping("/upload")
public String handleFileUpload(@RequestParam("files") MultipartFile[] files, Model model) {
boolean allSuccessful = true; // 假设所有文件上传都成功,根据实际情况修改此逻辑
for (MultipartFile file : files) {
// 你的文件上传逻辑...
if (!uploadSuccessful) {
allSuccessful = false;
break;
}
}
if (allSuccessful) {
model.addAttribute("successMessage", "所有文件上传成功!");
} else {
model.addAttribute("errorMessage", "文件上传过程中发生错误!");
}
return "uploadResult"; // 返回到展示结果的Thymeleaf页面
}
}
前端thymeleaf
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>文件上传结果</title>
</head>
<body>
<!-- 成功提示 -->
<div th:if="${successMessage}" class="alert alert-success" role="alert">
<span th:text="${successMessage}"></span>
</div>
<!-- 失败提示 -->
<div th:if="${errorMessage}" class="alert alert-danger" role="alert">
<span th:text="${errorMessage}"></span>
</div>
</body>
</html>