使用SpringMVC框架实现文件上传
1.配置上传文件解释器
(由于springMVC文件上传功能引用了commons-fileupload组件,所以记得导入commons-fileupload和commons-io包)
<!—配置文件上传解释器 --> <mvc:annotation-driven></mvc:annotation-driven> <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxInMemorySize" value="512000000" ></property> <property name="maxUploadSize" value="20000000"></property> </bean>
2.在Controller层编写映射方法
@RequestMapping(value="upload")
public String upload(MultipartFile file) throws Exception{
File destfile = new File("D:/dir/" + file.getOriginalFilename());
file.transferTo(destfile);
return "/upload.jsp";
}