以上传一个图片为例

添加依赖

commons-fileuploadcommons-fileupload1.3.1

配置文件上传解析器

测试

@Controller@RequestMapping("/account")public class AccountController {@RequestMapping(path="/upload")public String upload(HttpServletRequest request,MultipartFile upload,Model model) throws IOException {System.out.println("springmvc方式的文件上传");//获取要上传的文件目录String path = request.getSession().getServletContext().getRealPath("/uploads");System.out.println("path:"+path);//根据文件上传的目录创建File对象,如果不存在则创建1个File对象File file = new File(path);if(!file.exists()){//创建一个file对象file.mkdirs();}//获取文件上传名称String filename = upload.getOriginalFilename();//完成文件上传upload.transferTo(new File(path,filename));model.addAttribute("msg", "欢迎你 springmvc");return "success";}}

在index.jsp里面定义超链接

文件: 

测试结果

1.上传文件前

2.上传文件后