Spring mvc file Upload download

Source: Internet
Author: User

---restore content starts---

File upload download When the project development most commonly used features, upload files when the form must be set up as follows:

    1. Set method to post
    2. and set the enctype to Multipart/data

This allows the browser to send the user's selected file binary data to the server

Springle MVC provides direct support for file uploads, and this support is implemented with Multipartresolver, SPRINGMVC upload relies on Apache Commons fileupload support

That requires a Commons-fileupload-xx.jar rack package,

Here's a small example of uploading first.

1. First, configure the Multipartresolver in the configuration file

 <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">          <!--Maximum upload file size in bytes (10MB) -        < Propertyname= "Maxuploadsize">              <value>10485760</value>          </ Property>          <!--the requested encoding format must be consistent with the Pageencoding property of the JSP in order to correctly read the contents of the form, default to Iso-8859-1 -        < Propertyname= "Defaultencoding">            <value>UTF-8</value>        </ Property>    </Bean>

2. Front Page

<Body>    <H2>Spring MVC File Upload</H2>    <formAction= "Upload"enctype= "Multipart/form-data"Method= "POST">        <Table>            <TR>                <TD>File description</TD>                <TD><inputtype= "text"name= "description"/></TD>            </TR>            <TR>                <TD>Please select a file</TD>                <TD><inputtype= "File"name= "File"></TD>            </TR>            <TR>                <TD><inputtype= "Submit"value= "Upload"></TD>            </TR>        </Table>    </form></Body></HTML>

3. Upload Method

@RequestMapping (value= "/{formname}")      Publicstring LoginForm (@PathVariable string formName) {//Dynamic Jump Page        returnFormName; }        //The upload file is automatically bound to the Multipartfile@RequestMapping (value= "/upload", method=requestmethod.post) PublicString Upload (httpservletrequest request, @RequestParam ("description") String description, @RequestParam ("File") Multipartfile file, model model)throwsexception{System.out.println (description); //If the file is not empty, write the upload path            if(!File.isempty ()) {                //Upload file pathString Path =Request.getservletcontext (). Getrealpath ("/images/"); //Upload file nameString filename =File.getoriginalfilename (); File filepath=NewFile (path,filename); //determine if the path exists, and if it does not exist, create a                if(!Filepath.getparentfile (). exists ()) {System.out.println ("Path does not exist");                Filepath.getparentfile (). Mkdirs ();                } System.out.println (Filepath.getparentfile ()); //Save the uploaded file to a target fileFile.transferto (NewFile (path+file.separator+filename)); return"Success"; }Else{                return"Error"; }                      }    

Spring MVC binds the uploaded file to Multipartfile, Multipartfile provides a way to get the contents of the uploaded file, the file name, and the Transto () method to store the file in hardware, Multipartfile

The usual methods are as follows:

    1. Byte[] GetByte (): Get File
    2. String getContentType (): Gets the file MIME type, such as Image/jpeg
    3. InputStream getinputstream () get file stream
    4. String getName (). Get the name of the file component in the form
    5. String Getoriginalfilename () Gets the original of the uploaded file
    6. Long GetSize (): Get file byte size
    7. Boolean isEmpty (): Whether there is an upload file
    8. void TransferTo (File dest): Save the file to a destination file

Spring mvc file Upload download

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.