多文件上传
方式一:多个上传input,后端多个接收
<form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
名称:<input type="text" name="username"><br>
文件1:<input type="file" name="uploadFile"><br>
文件2:<input type="file" name="uploadFile2"><br>
<input type="submit" value="上传文件">
</form>
后端代码实现
@RequestMapping("quick23")
@ResponseBody
public void save23(String username, MultipartFile uploadFile, MultipartFile uploadFile2) throws IOException {
System.out.println(username);
String filename = uploadFile.getOriginalFilename();
String filename2 = uploadFile2.getOriginalFilename();
uploadFile.transferTo(new File("C:\\Users\\zhong\\Desktop"+filename));
uploadFile2.transferTo(new File("C:\\Users\\zhong\\Desktop"+filename2));
}
方式二:多个上传input,同一个name,后端使用MultipartFile[]数组接收
前端代码实现
<form action="${pageContext.request.contextPath}/user/quick24" method="post" enctype="multipart/form-data">
名称:<input type="text" name="username"><br>
文件1:<input type="file" name="uploadFile"><br>
文件2:<input type="file" name="uploadFile"><br>
<input type="submit" value="上传文件">
</form>
后端代码接收实现
@RequestMapping("quick24")
@ResponseBody
public void save24(String username, MultipartFile[] uploadFile) throws IOException {
System.out.println(username);
for (MultipartFile multipartFile : uploadFile) {
String originalFilename = multipartFile.getOriginalFilename();
multipartFile.transferTo(new File("C:\\Users\\zhong\\Desktop"+originalFilename));
}
}