File uploads in spring MVC

Source: Internet
Author: User

The required racks for uploading files using Commons-fileupload are: Commons-fileupload and Common-io two Rack pack support, which can be smashed on the Apache website.

To configure uploads in profile Spring-mvc.xml:

<id= "Multipartresolver"        class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">//File Upload max is how much
        <name= "Maxuploadsize"  value= "10485760000"/ >//Upload file encoding method
        <name= "defaultencoding"  value= "Utf-8"/>// Maximum cache size in memory
        <name= "Maxinmemorysize"  value= "40960"/>  </bean>

In the corresponding JSP page there are two main points to note, asked form form the method= "post" and enctype= "Multipart/form-data", the others are usually written.

The corresponding code in the controller that is submitted to spring MVC is as follows:

@RequestMapping (value= "/upload.do", method=requestmethod.post)    publicthrows  ioexception{        for(multipartfile file:files) {            if(!  File.isempty ()) {            new File ("E:/file" +file.separator+file.getoriginalfilename ())}        }         return "Uploadok";    }

Adding @requestparam code in the main is to deal with multiple files when uploading, if it is a single file upload can not be written.

File uploads in spring MVC

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.