Spring MVC provides direct support for file uploads, which is implemented through plug-and-play multipartresolver . Spring with Jakarta Commons FileUpload Technology implements a Multipartresolver implementation class:Commonsmultipartresovler
Multipartresovleris not assembled by default in the spring MVC context, so uploading of files is not handled by default, and if you want to use the spring file upload feature, Need to configure Multipartresolver in context now
ConfigurationMultipartresolver
defaultencoding: must be consistent with the user JSP 's pageencoding property in order to correctly parse the contents of the form
? In order for Commonsmultipartresovler to work correctly, you must first add Commons FileUpload and Commons io class packages to the classpath.
Pom.xml:
<!--uploading Files--><dependency><groupid>commons-fileupload</groupid><artifactid> Commons-fileupload</artifactid><version>1.2.1</version></dependency><dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId><version> 2.1</version></dependency>
Springmvc-xml
<!--spring mvc file uploads--><bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "><property name=" defaultencoding " Value= "UTF-8"/><property name= "maxinmemorysize" value= "10240"/> <!--maximum memory size (10240)- < Property Name= "Maxuploadsize" value= "-1"/> <!--maximum file size, 1 for Infinity ( -1)- </bean>
Example
Package Com.ibigsea.springmvc.controller;import Java.io.file;import Java.io.ioexception;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import org.springframework.web.multipart.multipartfile;@ Controllerpublic class Fileuploadcontroller {@RequestMapping ("/upload") public String Upload (@RequestParam ("file") Multipartfile file) throws IllegalStateException, Ioexception{if (!file.isempty ()) {System.out.println ( File.getoriginalfilename ()); File.transferto (New File ("D:\\files\\" + File.getoriginalfilename ())); return "HelloWorld";}}
SPRINGMVC Study notes (eight) file upload