1. 编写 Controller
@RestController
public class FileUpload {
@RequestMapping("/fileUpload")
@ResponseBody
public Map<String,Object> fileUpload(MultipartFile file) throws IOException {
System.out.println(file.getOriginalFilename());
file.transferTo(new File("g:/"+file.getOriginalFilename()));
Map<String,Object> map = new HashMap<>();
map.put("msg","OK");
return map;
}
}
2. 表单
<form action="/fileUpload" method="post" enctype="multipart/form-data">
<input name="file" type="file" >
<input type="submit" value="提交">
</form>
3. 启动类
@EnableAutoConfiguration
@ComponentScan("com.zth")
public class App {
public static void main(String[] args){
// 启动 SpirngBoot 项目
SpringApplication.run(App.class,args);
}
}
4. 设置上传文件大小的默认值
application.properties 中添加配置:
#设置单个上传文件的大小
spring.http.multipart.maxFileSize=200MB
#设置一次请求上传文件的总容量
spring.http.multipart.maxRequestSize=200MB