Springboot file Upload, download, set size

Source: Internet
Author: User
Tags save file

The version of this article using Springboot is 2.0.3.RELEASE

1. Upload a single file

①html the corresponding submission form

 <formAction= "UploadFile"Method= "POST"enctype= "Multipart/form-data">        <P>Select File:<inputtype= "File"name= "FileName"/></P>        <P><inputtype= "Submit"value= "Submit"/></P> </form>

The processing code in ②boot. It's important to note

@RequestMapping ("/uploadfile") corresponds to action= "UploadFile"

@RequestParam ("filename") corresponds to name= "filename"

@RequestMapping ("/uploadfile") @ResponseBody PublicString UploadFile (@RequestParam ("FileName") multipartfile file) {//determine if the file is empty        if(File.isempty ()) {return"-1"; } String fileName=File.getoriginalfilename (); //add a timestamp and try to avoid duplicate file namesString Path = "d:/" +NewSimpleDateFormat ("Yyyymmddhhmmss"). Format (NewDate ()) + "_" +FileName; File dest=NewFile (path); //determine if the file already exists        if(Dest.exists ()) {return"-2"; }        //determine if the file parent directory exists        if(!Dest.getparentfile (). exists ())        {Dest.getparentfile (). mkdir (); }        Try{File.transferto (dest);//Save File}Catch(IOException e) {return"-3"; }        return"0"; }

2. Uploading Multiple Files

①html the corresponding submission form

    <formAction= "Uploadmultifile"Method= "POST"enctype= "Multipart/form-data" >        <P>Select File 1:<inputtype= "File"name= "FileName"/></P>        <P>Select File 2:<inputtype= "File"name= "FileName"/></P>        <P>Select File 3:<inputtype= "File"name= "FileName"/></P>        <P><inputtype= "Submit"value= "Submit"/></P>    </form>

The processing code in ②boot.

    @RequestMapping ("/uploadmultifile")    @ResponseBody    public String keywordsubmitfile (@ Requestparam ("FileName") list<multipartfile> files) {        for  (multipartfile file:files ) {            // Here is a simple output file name             System.out.println (File.getoriginalfilename ());        }         return "0";    }

3. Download the file

@RequestMapping ("/downloadfile") @ResponseBody PublicString DownloadFile (httpservletresponse response, @RequestParam ("FileName") String filepathname) {File file=NewFile (filepathname); if(!file.exists ()) {            return"-1";        } response.reset (); Response.setheader ("Content-disposition", "attachment;filename=" +filepathname); Try{InputStream instream=NewFileInputStream (filepathname); OutputStream OS=Response.getoutputstream (); byte[] Buff =New byte[1024]; intLen =-1;  while(len = instream.read (buff)) > 0) {os.write (buff,0, Len);            } os.flush ();            Os.close ();        Instream.close (); } Catch(Exception e) {e.printstacktrace (); return"-2"; }        return"0"; }

4. Set the upload download file size

① depending on the version, the corresponding setting value is different

Spring Boot 1.3.x and earlier
    • multipart.maxFileSize
    • multipart.maxRequestSize
Spring Boot 1.4.x and 1.5.x
    • spring.http.multipart.maxFileSize
    • spring.http.multipart.maxRequestSize
Spring Boot 2.x
    • spring.servlet.multipart.maxFileSize
    • spring.servlet.multipart.maxRequestSize

② For example, in the 2.x version, set the 30MB size in the project's Application.properties file

spring.servlet.multipart.maxfilesize=30mbspring.servlet.multipart.maxrequestsize=30MB

If you do not limit the size, set to 1

Spring.servlet.multipart.maxfilesize=-1spring.servlet.multipart.maxRequestSize=-1

Reference:

Spring Boot Primer-File upload and download

"I am trying to set maxfilesize but it's not honored"

Above.

Springboot file Upload, download, set size

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.