文件上传之使用一个属性接收多个文件

在开发过程中,可能遇到这样的业务:文件上传时个数不定,这样我们不能枚举出所有的文件name,这种情况下我们可以使用一个name将所有的文件接收下来;

html代码
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8021/xxx/upload1" method="post" enctype="multipart/form-data">

    <input type="file" name="file"><br/>
    <input type="file" name="file"><br/>
    <input type="file" name="file"><br/>
    <input type="file" name="file"><br/>
    <input type="file" name="file"><br/><br/>
    <button type="submit">提交</button>
</form>

</body>
</html>
controller代码

使用springboot或者spring mvc框架

复制代码
    @PostMapping("/upload1")
    public String up(@RequestPart(value = "file") List<MultipartFile> fileList) {
        fileList.forEach(a -> {
            System.out.println(a.getOriginalFilename() + "-->" + a.getSize());
        });
        return "success";
    }

这样就接收到了所有文件

over~~

相关推荐
代码漫谈14 分钟前
JVM 参数调优:Spring Boot与JDK新特性的最佳结合
java·jvm·spring boot
北风朝向18 分钟前
springboot使用@Validated校验List接口参数
spring boot·后端·list·校验·valid
海兰32 分钟前
【第39篇】spring-ai-alibaba-graph-example学习路径概览
人工智能·spring boot·学习·spring·spring ai
Don.TIk39 分钟前
天机の学堂
java·spring boot·spring·maven·mybatis
Devin~Y1 小时前
大厂Java面试实录:Spring Boot/JPA/Redis/Kafka/K8s 可观测性 + Spring AI RAG/Agent(小Y翻车现场)
java·spring boot·redis·mybatis·hibernate·spring mvc·jpa
SuperherRo1 小时前
服务攻防-开发框架安全&ThinkPHP&Laravel&SpringBoot&Struts2&SpringCloud&复现
spring boot·laravel·thinkphp·struts2·框架安全
消失的旧时光-19434 小时前
SQL 第五篇:SQL 如何真正接入 Spring Boot 项目(企业 Mapper 分层实战)
数据库·spring boot·sql
想学习java初学者11 小时前
SpringBoot整合Vertx-Mqtt多租户(优化版)
java·spring boot·后端
生活真难17 小时前
SpringCloud - 任务调度 - xxl-job-java
java·spring boot·spring cloud
直奔標竿19 小时前
MySQL与Redis数据一致性实战方案(避坑指南)
java·数据库·spring boot·redis·mysql·spring·缓存