Controller
@Slf4j
@Controller
public class FormController {
@GetMapping("upload")
public String form(){
return "form/upload";
}
@PostMapping("upload")
public String upload(@RequestParam("email") String email,
@RequestParam("username") String username,
@RequestPart("headerImg")MultipartFile headerImg,
@RequestPart("photos") MultipartFile[] photos) throws IOException {
log.info("上传的文件信息,email={},username={},HeaderImg={},photos={}",
email,username,headerImg.getSize(),photos.length);
if (!headerImg.isEmpty()){
String originalFilename = headerImg.getOriginalFilename();
headerImg.transferTo(new File("D:\\"+originalFilename));
}
return "dashboard";
}
}
form
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
<div>
<label>邮箱</label>
<input type="email" name="email">
</div>
<div>
<label>用户名</label>
<input type="text" name="username">
</div>
<div>
<label>头像</label>
<input type="file" name="headerImg">
</div>
<div>
<label>生活照</label>
<input type="file" name="photos" multiple="multiple">
</div>
<input type="submit">
</form>
配置文件
spring.servlet.multipart.max-file-size=30MB
spring.servlet.multipart.max-request-size=50MB