SPRINGMVC Study notes (eight) file upload

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.