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

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

相关推荐
Devin~Y3 小时前
大厂Java面试实录:Spring Boot微服务 + Redis/Kafka + Prometheus/Jaeger + RAG/Agent(小Y水货版)
java·spring boot·redis·spring cloud·kafka·prometheus·jaeger
user_admin_god3 小时前
Spring Boot 3 + WebFlux 企业级流式SSE接口最佳实践
java·spring boot·后端
怪祝浙3 小时前
spring boot的启动原理以及mvc和ssm的解释
spring boot·后端·mvc
是梦终空19 小时前
计算机源码273—基于SpringBoot+Vue3停车场管理系统带支沙箱支付(源代码+数据库)
数据库·spring boot·vue·mybatis·停车场管理系统·沙箱支付·毕设设计
Filwaod19 小时前
Java面试:AIGC场景下的技术深度拷问-谢飞机篇
spring boot·缓存·微服务·消息队列·aigc·java面试·ai技术
隐退山林19 小时前
JavaEE进阶:SpringBoot配置文件
java·spring boot·java-ee
霸道流氓气质1 天前
SpringAI+Ollama本地模型实现快速对话入门实例
spring boot·ai
xun-ming1 天前
SpringBoot和Vue3实战阿里百炼大模型极简版
spring boot·ai·vue3·智能体·百炼大模型
csdn2015_1 天前
java springboot 文件导入,判断第一列的值是否有重复
java·windows·spring boot
阿丰资源1 天前
基于Springboot+mysql的在线兼职平台(附源码)
spring boot·后端·mysql