1. 文件上传
1.1 文件上传客户端三要素
- 表单项 type = “file”
- 表单的提交方式是 post
- 表单的 enctype 属性是多部分表单形式,即 enctype = “multipart/form-data”
1.2 文件上传原理
1.3 单文件上传步骤
- 导入fileupload和io坐标
- 配置文件上传解析器
- 编写文件上传代码
1.4 单文件上传实现
编写代码上传文件:
@RequestMapping("/quickstart22")
@ResponseBody
public void save22(String username, MultipartFile upload) throws IOException {
System.out.println(username);
//获得上传文件的名称
String originalFilename = upload.getOriginalFilename();
//保存文件
upload.transferTo(new File("E:\\upload\\"+originalFilename));
}
1.5 多文件上传实现
@RequestMapping("/quickstart22")
@ResponseBody
public void save22(String username, MultipartFile[] uploads) throws IOException {
System.out.println(username);
for (MultipartFile upload : uploads) {
String originalFilename = upload.getOriginalFilename();
upload.transferTo(new File("E:\\upload\\"+originalFilename));
}
}