一、enctype和method要设置成图片样式
form表单如下:
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Email</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="inputEmail1" placeholder="Email">
<p class="help-block">Example block-level help text here.</p>
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 col-sm-2 control-label">Password</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassword1"
placeholder="Password">
<!--<p class="help-block">Example block-level help text here.</p>-->
</div>
</div>
<div class="form-group">
<label for="exampleInputFile2" class="col-lg-2 col-sm-2 control-label">File
input</label>
<div class="col-lg-10">
<input type="file" id="exampleInputFile2">
<p class="help-block">Example block-level help text here.</p>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
二、在控制器类中编写方法
/*文件上传*/
@PostMapping("/upload")
public String upload(String email, String uname, MultipartFile file,MultipartFile[] files) throws IOException {
System.out.println("邮箱=="+email+"用户名=="+uname);
System.out.println("单文件=="+file.getName());
System.out.println("多文件=="+files.length);
File path = new File("G://images");
if(!path.exists()){
path.mkdir();
}
if(!file.isEmpty()){
file.transferTo(new File(path,"//"+file.getOriginalFilename()));
}
for(MultipartFile item:files){
if(!item.isEmpty()){
item.transferTo(new File(path,"//"+item.getOriginalFilename()));
}
}
return "redirect:/main.html";
}
三、测试实验