客户端
<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputEmail1">邮箱</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1"
placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">名字</label>
<input type="text" name="userName" class="form-control" id="exampleInputPassword1"
placeholder="Password">
</div>
<div class="form-group">
<label for="headImage">头像</label>
<input type="file" name="headImage" id="headImage">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="form-group">
<label for="photos">File input</label>
<input type="file" name="photos" id="photos" multiple>
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
服务端
@Log4j2
@Controller
public class UpLoadController {
@PostMapping("/upload")
public String upLoad(@RequestParam("email") String email,
@RequestParam("userName") String userName,
@RequestPart("headImage") MultipartFile headImage,
@RequestPart("photos") MultipartFile[] photos) {
log.info("上传信息:email:{},userName:{},headImage:{},photos:{}", email, userName, headImage.getSize(), photos.length);
return "form/form_layouts";
}
}
设置单个文件大小限制、总体文件大小限制
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-file-size=100MB