spring-web.xml中的配置:
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="5242880"></property>
</bean>
随便来个控制层:
@RequestMapping("/test4")
public String upload(@RequestParam("name") String name,@RequestParam("file1") MultipartFile file1) throws IllegalStateException, IOException {
if(!file1.isEmpty()) {
System.out.println(name);
file1.transferTo(new File("d:\\temp\\"+file1.getOriginalFilename()));
}
return "/Ajax.jsp";
}
前端代码:
<form action="/springmvc/third/test4" method="post" enctype="multipart/form-data">
姓名:<input name="name"/>
文件:<input type="file" name="file1"/>
<input type="submit" value="测试4"/>
</form>