导包:
<!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
添加文件上传解析器:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"/> </bean>
编写控制器
//需求:文件上传 @RequestMapping("show17") public ModelAndView show17(@RequestParam("file") MultipartFile mf) throws IOException { ModelAndView mv = new ModelAndView("hello"); if(mf!=null){ mf.transferTo(new File("e:/test/"+mf.getOriginalFilename())); } mv.addObject("msg","上传成功:"+mf.getOriginalFilename()); return mv; }
用psotman模拟发送文件
结果: