SPRINGMVC Study Record (iv)--File upload

Source: Internet
Author: User
Tags file handling mul

Learning a framework and learning file upload

When using SPRINGMVC for system implementation, the SPRINGMVC default parser is not added to the file upload parsing, which can facilitate our implementation of their own file upload. However, if you want to use Springmvc file upload parser to handle file uploads, you need to add SPRINGMVC applicationcontext inside the spring to provide the Multipartresolver declaration. Then, each time the client makes a request, SPRINGMVC checks to see if it contains multimedia information, and if it does, it uses Multipartresolver to parse it. SPRINGMVC uses a multiparthttpservletrequest that supports file handling to wrap the current httpservletrequest, You can then use Multiparthttpservletrequest to process the file.

1. Introducing a Dependency Package

File upload requires additional ingestion of packages, respectively

    1. Commons-fileupload-1.3.1.jar
    2. Commons-io-2.4.jar
2. Configure JSP pages
<%@ page contenttype="Text/html;charset=utf-8" language="java" %><% @taglib prefix="SF" uri="Http://www.springframework.org/tags/form "%><html><head>    <meta charset="Utf-8">    <title>User Login</title></head><body>    <%--file Upload words need enctype="Multipart/form-data"--%>    <sf:form modelattribute="User" method="POST" enctype ="Multipart/form-data">User name:<sf:input path="username"/><sf:errors path="username"/>        <br>Password:<sf:input path="password"/><sf:errors path="password"/>        <br>Nickname:<sf:input path="nickname"/><sf:errors path="nickname"/>        <br>        <%--here to set file upload--%>File:<input type="file" name="file">        <input type="Submit" value="Submit">    </sf:form></body></html>
3. Configure the appropriate controller
@RequestMapping(Value ="/login", method = Requestmethod.post) PublicStringLogin(@Validated user User, Bindingresult br, Model model,@Requestparam("File") Multipartfile file) {if(Br.haserrors ()) {return "User/login"; }//Get variable name---file type---filenameSystem.out.println (File.getname () +"---"+file.getcontenttype () +"---"+file.getoriginalfilename ());Try{if(!file.isempty ()) {//Copy files using STREAMSAPI modeStreams.copy (File.getinputstream (),NewFileOutputStream ("e:/temp/"+file.getoriginalfilename ()),true); }        }Catch(IOException e) {System.out.println ("File upload failed");        E.printstacktrace (); } System.out.println (User.tostring ());return "User/login"; }

This completes the upload of a single file.

4. Multi-File Upload

Multi-File upload is simple, just need to change the parameters to the array can be

    @RequestMapping(Value ="/login", method = Requestmethod.post) PublicStringLogin(@Validated user User, Bindingresult br, Model model,@Requestparam("File") multipartfile[] file) {if(Br.haserrors ()) {return "User/login"; }//The file is traversed here         for(Multipartfile mul:file) {//Get variable name---file type---filenameSystem.out.println (Mul.getname () +"---"+mul.getcontenttype () +"---"+mul.getoriginalfilename ());Try{if(!mul.isempty ()) {streams.copy (Mul.getinputstream (),NewFileOutputStream ("e:/temp/"+mul.getoriginalfilename ()),true); }            }Catch(IOException e) {System.out.println ("File upload failed");            E.printstacktrace (); }} System.out.println (User.tostring ());return "User/login"; }

SPRINGMVC Study Record (iv)--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.