spring-sevlet.xml
uploadController / .jsp
web.mxl
spring org.springframework.web.servlet.DispatcherServlet 1 spring *.do org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-INF/spring-servlet.xml index.jsp
UploadBean.java
package com.mxl.upload;import org.springframework.web.multipart.MultipartFile;public class UploadBean { private MultipartFile file; private String filename; public MultipartFile getFile() { return file; } public void setFile(MultipartFile file) { this.file = file; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } }
UploadController.java
package com.mxl.upload;import java.io.File;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;import org.springframework.web.util.WebUtils;public class UploadController extends SimpleFormController { protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,Object command,BindException errors) throws Exception{ String path=WebUtils.getRealPath(request.getSession().getServletContext(),"/"); UploadBean upBean=(UploadBean) command; upBean.setFilename(upBean.getFile().getOriginalFilename()); upBean.getFile().transferTo(new File(path+"/upload/"+upBean.getFile().getOriginalFilename())); return new ModelAndView("success","bean",upBean); }}