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