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

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

相关推荐
Microsoft Word1 小时前
项目中后端如何处理异常?
java·spring boot·java-ee
代码老y1 小时前
Spring Boot + MyBatis + Vue:全栈开发的深度剖析与实践指南
vue.js·spring boot·mybatis
weixin_438335402 小时前
Springboot集成阿里云OSS上传
spring boot·阿里云
天上掉下来个程小白2 小时前
Apache ECharts-02.入门案例
前端·spring boot·apache·echarts·苍穹外卖
程序员岳焱3 小时前
Spring Boot开发十大常见“坑”深度解析与避坑指南
java·spring boot·后端
风象南3 小时前
SpringBoot实现面对面建群(基于Redis)
java·spring boot·后端
优创学社24 小时前
springboot口腔管理平台
java·spring boot·后端
paopaokaka_luck4 小时前
基于SpringBoot+Uniapp的活动中心预约小程序(协同过滤算法、腾讯地图、二维码识别)
java·vue.js·spring boot·小程序·uni-app
荔枝吻5 小时前
【沉浸式解决问题】Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
spring boot