Springboot---Web application development-File upload

Source: Internet
Author: User

First, Spring Boot by default using SPRINGMVC wrapped parser for uploading

Second, add code

<form method= "POST" enctype= "Multipart/form-data" action= "/file/upload" > Files: <input type= "file" Name= " Roncoofile "/> <input type=" Submit "value=" Upload "/> </form> @Controller @RequestMapping (value ="/file ") pub Lic class Filecontroller {private static final Logger Logger = Loggerfactory.getlogger (filecontroller.class); @RequestMa Pping (value = "Upload") @ResponseBody public String upload (@RequestParam ("Roncoofile") multipartfile file) {if (file.ise Mpty ()) {return "file is empty";}//Get file name String filename = File.getoriginalfilename (); Logger.info ("uploaded file name:" + fileName); Gets the suffix name of the file String suffixname = filename.substring (Filename.lastindexof (".")); Logger.info ("uploaded suffix named:" + Suffixname); File upload path String filePath = "d:/roncoo/ttt/"; Solve Chinese problem, liunx Chinese path, picture display problem//FileName = Uuid.randomuuid () + suffixname; File Dest = new file (FilePath + fileName); Detects if there is a directory if (!dest.getparentfile (). exists ()) {Spring Boot basic tutorial Feng Yongwei 2 Dragon Fruit Academy: http://www.roncoo.com dest.getparentfile (). Mkdirs (); } try {File.transferto (dest); return "Upload succeeded";} catch (IllegalStateException e) {e.printstacktrace ();} catch (Ioexceptio n e) {e.printstacktrace ();} return "Upload failed";  }

  


}
Third, the configuration
Spring.http.multipart.enabled=true #默认支持文件上传.
Spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘.
spring.http.multipart.location= # Temporary directory for uploading files
SPRING.HTTP.MULTIPART.MAX-FILE-SIZE=1MB # maximum supported file size
SPRING.HTTP.MULTIPART.MAX-REQUEST-SIZE=10MB # maximum Support request size

Springboot---Web application development-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.