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