First step: Import Commons-fileupload-1.3.1.jar and Commons-io-2.2.jar rack packages
Step two: Configure in Applicationcontext.xml
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
Step three: Add properties to page <form> tag enctype= "Multipart/form-data" This is a must for uploading files
An easy way to do this is as follows
1.web.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app version= "3.0"3Xmlns= "Http://java.sun.com/xml/ns/javaee"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6http//java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">7<display-name></display-name>8<welcome-file-list>9<welcome-file>index.jsp</welcome-file>Ten</welcome-file-list> One A<!--Configuring the PRINGMVC front-end controller- -<servlet> -<servlet-name>dispatcherServlet</servlet-name> the<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> -<!--setting SPRINGMVC profile-- -<init-param> -<param-name>contextConfigLocation</param-name> +<param-value>/WEB-INF/shopping-servlet.xml</param-value> -</init-param> +<load-on-startup>1</load-on-startup> A</servlet> at<servlet-mapping> -<servlet-name>dispatcherServlet</servlet-name> -<url-pattern>/</url-pattern> -</servlet-mapping> -</web-app>
2.shopping-servlet.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:p= "http://www.springframework.org/schema/p"5xmlns:context= "Http://www.springframework.org/schema/context"6xsi:schemalocation="7http//Www.springframework.org/schema/beans8http//www.springframework.org/schema/beans/spring-beans.xsd9http//Www.springframework.org/schema/contextTenhttp//www.springframework.org/schema/context/spring-context.xsd "> One A<!--let spring container automatically detect bean Controller Service respoise componet -<!-- -<beanclass= "Org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" ></bean> the- - -<context:component-scan base- Package= "Com.bdqnsjz.shopping.controller" ></context:component-scan> - +<bean id= "Multipartresolver" - class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxuploadsize= "34564356345456" P: Maxuploadsizeperfile= "3456435634545" p:defaultencoding= "Utf-8" > +<!-- A<property name= "defaultencoding" value= "Utf-8" ></property> at- -</bean> - - -<!--configuration View Resolver-- -<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > in<property name= "prefix" value= "/web-inf/pages/" ></property> -<property name= "suffix" value= ". JSP" ></property> to</bean> + - the *</beans>
3.controller
1@RequestMapping (value= "/upload1", method=requestmethod.post)2 PublicString Upload1 (@RequestParam (value = "File", required =false) multipartfile file,3 HttpServletRequest Request) {4 5String FileName =file.getoriginalfilename ();6 System.out.println (fileName);7 8 //Get extension9String extension = filename.substring (Filename.lastindexof (".")));Ten One //new file name 234-135234532l34kjlkj.jpg AString savedfilename = Java.util.UUID.randomUUID (). toString () +extension; - -String FolderPath = Request.getrealpath ("/web-inf/upload"); the -File Savedfile =NewFile (FolderPath, savedfilename); - - + //Save File - Try { + File.transferto (savedfile); A}Catch(IOException e) { at e.printstacktrace (); - } - - return"Upload1"; -}
Uploading files in Springmvc (annotations)