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

在开发过程中,可能遇到这样的业务:文件上传时个数不定,这样我们不能枚举出所有的文件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~~

相关推荐
王翼鹏1 小时前
Spring boot 策略模式
java·spring boot·策略模式
欧阳有财2 小时前
[java八股文][JavaSpring面试篇]SpringBoot
java·spring boot·面试
Wilson Chen6 小时前
告别硬编码!用工厂模式优雅构建可扩展的 Spring Boot 应用 [特殊字符]
java·spring boot·spring
crud6 小时前
Spring Boot 定时任务全攻略:从入门到实战,一篇文章讲清楚!
spring boot
风象南8 小时前
SpringBoot数据转换的4种对象映射方案
java·spring boot·后端
JAdroid9 小时前
spring-boot redis lua脚本实现滑动窗口限流
数据库·spring boot·redis·spring·lua·redisson
Code哈哈笑9 小时前
【基于SpringBoot的图书购买系统】Redis中的数据以分页的形式展示:从配置到前后端交互的完整实现
java·spring boot·redis·后端·spring·交互
zqmattack18 小时前
解决idea与springboot版本问题
java·spring boot·intellij-idea
酱学编程19 小时前
【监控】Spring Boot 应用监控
java·spring boot·后端·prometheus
MyikJ20 小时前
Java互联网大厂面试:从Spring Boot到Kafka的技术深度探索
java·spring boot·微服务·面试·spark·kafka·spring security