Spring Multi-File upload multipart configuration

Source: Internet
Author: User
Tags file upload tomcat

SPRINGMVC multi-File upload multipart configuration first, the project introduces the dependency jar package configuration Spring support multipart file upload Controller processing mapping foreground upload file Finally we can look at the difference between a multipart request and a normal request

springmvc multiple file uploads: multipart configuration 1. First, the project introduces a dependency jar package:

The commons bag is used here. I think it is more useful to use.
Pom.xml

        <!--upload Components package--
        <dependency>
            <groupId>commons-fileupload</groupId>
            < artifactid>commons-fileupload</artifactid>
            <version>1.3.1</version>
        </ dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId> commons-io</artifactid>
            <version>2.4</version>
        </dependency>
        < dependency>
            <groupId>commons-codec</groupId>
            <artifactid>commons-codec</ artifactid>
            <version>1.11</version>
        </dependency>
2. Configure Spring Support multipart file upload:

Spring's XML Classic configuration file:
Configure Multipartresolver

    <!--configuration file upload, if you do not use file upload can not be configured, of course, if not, then the configuration file does not have to introduce the upload component package--
    <bean id= "multipartresolver"
        class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver >
        <!--Default code--
        <property Name= "defaultencoding" value= "Utf-8"/>
        <!--file size max--
        <property name= "maxuploadsize" value= " 10485760000 "/>
        <!--max in memory--
        <property name=" maxinmemorysize "value=" 40960 "/>
    </ Bean>
3.Controller Processing Mappings

The controller handles the more critical parts of the mapping.

    @RequestMapping (consumes = "Multipart/form-data", value = "/awards-create", method = requestmethod.post) public
    Modelandview awardscreate (httpservletrequest request,modelandview Mav,
            @RequestParam ("PictureFile1") Multipartfile pictureFile1) throws IOException {  
        String RootPath = System.getproperty ("catalina.home") + "/webapps_ Data ";

        if (! ( New file (RootPath). Exists ()) {
            //   ~/tomcat/webapps_data/If the    directory does not exist, the
            new file (RootPath) is created. mkdir () ;
        }

        Gets the type. such as. png   . jpg
        String file1type = Picturefile1.getoriginalfilename (). substring (
                Picturefile1.getoriginalfilename (). LastIndexOf ("."));

        String File1path = RootPath + "/" + Thesis.getid () + File1type;

        Using the method provided by Multipartfile, save directly to the file, see Source code
        Picturefile1.transferto (File1path);

At this point, we have configured OK, and then the reception of the wording: 4. The foreground upload file:

<label> Select File:</label>
<input type= "file" id= "PictureFile1" name= "PictureFile1" accept= "image/*" Class= "Required" aria-required= "true" >
Finally, we can look at the difference between a multipart request and a normal request:

Request Header

accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-encoding:gzip, deflate, BR
accept-language:zh-cn,zh;q=0.9
cache-control:max-age=0
Connection: Keep-alive
content-length:66719
content-type:multipart/form-data; boundary=---- webkitformboundaryqavwlflaawz5r40n
cookie:jsessionid=dc9979a274570b2891e7179ffe89eb9b
Host:localhost : 8080
origin:http://localhost:8080
referer:http://localhost:8080/****/user/awards-create
Upgrade-insecure-requests:1
user-agent:mozilla/5.0 (X11; Linux x86_64) applewebkit/537.36 (khtml, like Gecko) chrome/63.0.3239.84 safari/537.36

Request Payload

------webkitformboundaryqavwlflaawz5r40n
content-disposition:form-data; name= "name"

123213
------ webkitformboundaryqavwlflaawz5r40n
Content-disposition:form-data, name= "publishdate"

2018-04-06
--- ---webkitformboundaryqavwlflaawz5r40n
content-disposition:form-data, name= "Publishrange"

domestic and international public offering
-- ----webkitformboundaryqavwlflaawz5r40n
content-disposition:form-data; name= "picture01"; filename= " 2018-04-09 screenshot of 16-45-03. png "
content-type:image/png


------webkitformboundaryqavwlflaawz5r40n
Content-disposition:form-data; Name= "PICTURE02"; Filename= "2018-04-09 16-32-30 screenshot. png"
content-type:image/png


------ webkitformboundaryqavwlflaawz5r40n--
    @RequestMapping (consumes = "Multipart/form-data", value = "/awards-create", method = requestmethod.post) public Mod Elandview awardscreate (httpservletrequest request,modelandview Mav, @RequestParam ("PictureFile1") multipartfil

        E pictureFile1) throws IOException {... System.out.println ("| | | | | | | |
        ===================== "+ picturefile1.getcontenttype ()); System.out.println ("| | | | | | | |
        ===================== "+ picturefile1.getname ()); System.out.println ("| | | | | | | |

        ===================== "+ picturefile1.getoriginalfilename ());
        int L = picturefile1.getoriginalfilename (). Length (); System.out.println ("| | | | | | | |

        ===================== "+ L);
        int i = Picturefile1.getoriginalfilename (). LastIndexOf ("."); System.out.println ("| | | | | | | |
        ===================== "+ i); System.out.println ("| | | | | | | |
        ===================== "+ picturefile1.getoriginalfilename (). CharAt (L-1)); System.out.println ("| | | | | | | | ===================== "+ picturefile1.getoriginalfilename (). substring (i-1)); 
||||||| =====================image/png | | | | | | |
=====================picturefile1 | | | | | | |
=====================2018-04-14 09-54-19 screenshot. png | | | | | | |
=====================29 | | | | | | |
=====================25 | | | | | | |
=====================g
2018-04-14 10:08:30-45738 [http-bio-8080-exec-63] DEBUG   -Multipart file ' PictureFile1 ' with original filename [2018-04-14 09-54-19 screenshot. png], stored at [/home/gaoyisheng/devel/tomcat/ APACHE-TOMCAT-7.0.82/WORK/CATALINA/LOCALHOST/TRANS/UPLOAD_BAE19B7B_9B04_48B6_AAD4_51F530A4AE39_00000043.TMP]: Moved to [/home/gaoyisheng/devel/tomcat/tomcat/webapps_data/306_1.png]

Resources:

<< Spring in Action >> 4th edition

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.