Web page upload file, first need to understand the MIME multimedia type
Http://www.w3school.com.cn/media/media_mimeref.asp
Look at the Web. XML configuration to consolidate prior knowledge
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5"> <Display-name>WebTest</Display-name> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/config/applicationcontext.xml/web-inf/config/codeifaction.xml</Param-value> </Context-param> <servlet> <Servlet-name>Dispatcherservlet</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/config/codeifaction.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <!--intercept all requests that end with do - <servlet-mapping> <Servlet-name>Dispatcherservlet</Servlet-name> <Url-pattern>*.do</Url-pattern> </servlet-mapping> <welcome-file-list> <Welcome-file>Index.do</Welcome-file> </welcome-file-list></Web-app>
And then the Applicationcontext.xml.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd "Default-lazy-init= "true"> <!--start the Spring MVC Annotation feature to complete the mapping of requests and annotations Pojo - <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"Lazy-init= "false" /> <!--It is also best to add defaultannotationhandlermapping, otherwise it will be overwritten with XML or other mappings! - <Beanclass= "Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!--resolution of the Model view name, that is, adding a prefix to the Model view name - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"P:prefix= "/web-inf/jsp/"P:suffix= ". jsp" /> <!--support for uploading files - <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"/></Beans>
In spring, the file is wrapped as a multipart type.
Then the controller.
Packagecom.codeif.action;ImportJava.io.File;Importjava.util.Date;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.ModelMap;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;Importorg.springframework.web.multipart.MultipartFile; @Controller Public classuploadaction {@RequestMapping (value= "/upload.do") PublicString Upload (@RequestParam (value = "File", required =false) multipartfile file, httpservletrequest request, Modelmap model) {SYSTEM.OUT.PRINTLN (Start); String Path= Request.getsession (). Getservletcontext (). Getrealpath ("Upload");//gets the absolute path to the upload directory of the Web root directoryString fileName = File.getoriginalfilename ();//file name uploaded from the page//String fileName = new Date (). GetTime () + ". jpg";System.out.println (path); File TargetFile=NewFile (path, fileName); if(!targetfile.exists ()) {Targetfile.mkdirs (); } //Save Try{File.transferto (targetfile); } Catch(Exception e) {e.printstacktrace (); } model.addattribute ("FileUrl", Request.getcontextpath () + "/upload/" +fileName); return"Result"; }}
Finally, the Web Form type:
<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <title>Upload image</title> </Head> <Body> <formAction= "Upload.do"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "File"name= "File" /> <inputtype= "Submit"value= "Submit" /></form> </Body> </HTML>
Watch the enctype= "Multipart/form-data" here.
This is the setting of the mime of the request, the default is application/x-www-form-urlencoded, can not be used for file upload, only the use of multipart/form-data, to complete the transfer of file data, the following operation.
This completes the file upload
Uploadify plugin can be used to beautify upload
Spring MVC uploads files