springboot+ thymeleaf 实现文件上传

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




    文件上传结果


    
    

    
    
    
    

你可能感兴趣的:(spring,boot,后端,java)