文件上传并保存到相应的文件夹中
enctype(文件编码格式)="multipart/form-data是将文件以二进制的形式上传,这样可以实现多种类型的文件上传。-->
<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="exampleInputFile">头像</label>
<input type="file" name="headerImg" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="form-group">
<label for="exampleInputFile">生活照</label>
<input type="file" name="photos" multiple>
<!-- multiple表示多文件上传-->
<p class="help-block">Example block-level help text here.</p>
</div>
Controller中
@PostMapping("/upload")
public String upload(
@RequestParam("email") String email,//RequestParam将请求参数区域的数据映射到控制层方法的参数上
@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
);
//保存头像文件到文件服务器,阿里云服务器
/**
* Max file size.
* 单个文件大小不超过1MB
*/
// private DataSize maxFileSize = DataSize.ofMegabytes(1);
/**
* Max request size.
* 总上传文件数据大小不超过10MB
* 可以在配置文件中改
*/
// private DataSize maxRequestSize = DataSize.ofMegabytes(10);
if(!headerImg.isEmpty()){
String originalFilename=headerImg.getOriginalFilename();
headerImg.transferTo(new File("H:\\cache\\"+originalFilename));
}
if(photos.length>0){
for (MultipartFile photo: photos
) {
String originalFilename=photo.getOriginalFilename();
photo.transferTo(new File("H:\\cache\\"+originalFilename));
}
}
return "main";
}
- 表单中
<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
文件上传写法, - HTML标签中multiple表示多文件上传
- MultipartFile 这个类MultipartFile是spring类型,代表HTML中form data方式上传的文件,包含二进制数据+文件名称。一般是用来接受前台传过来的文件
- MultipartFile常用方法
1、文件类型值 getContentType()
2、表单中文件组件对应name值 getName()
3、文件原始名称包括后缀名 getOriginalFilename()
4、文件大小单位为k getSize()
5、文件是否为空 isEmpty()
6、保存到一个目标文件中 transferTo() - 文件的默认最大单个文件大小不超过1MB,总上传文件数据大小不超过10MB,这个数据大小可以在配置文件中更改
#更改请求单个最大文件和总请求文件大小
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
文件上传过程中用到了两个注解 @RequestPart和@RequestParam
他们之间的区别
6. @RequestPart这个注解用在multipart/form-data表单提交请求的方法上。
7. 支持的请求方法的方式MultipartFile,属于Spring的MultipartResolver类。这个请求是通过http协议传输的。
8. @RequestParam也同样支持multipart/form-data请求。
9. 他们最大的不同是,当请求方法的请求参数类型不再是String类型的时候。@RequestParam适用于name-value “String”类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML)例如:key = user, value = {“name”:“aaa”,“xx”:“xx”}。
10. @RequestParam适用于name-valueString类型的请求域,
@RequestPart适用于复杂的请求域(像JSON,XML)
比如上面的例子,前两个邮箱用户就是String类型的参数适用@RequestParam,后面时图片类型的也就是复杂的请求域用@RequestPart。