File uploads are inherently simple to configure in Spring-boot , but there is a problem with multiple file transfers and single delivery.
The two configurations are slightly different, and there are some things that make me this technology small white very easy to step on the pit.
Important points:
The upload is a single file: Multipartfile files
Multiple files uploaded: multipartfile[] File
To upload a background configuration from a single file :
Public Map UploadFile (@RequestParam ("file") Multipartfile File,httpservletrequest req) {//Note that the Multipartfile file represents the flyer Files Tempfile = new file (file upload directory); if (!tempfile.getparentfile (). exists ()) {Tempfile.getparentfile (). Mkdirs (); If the uploaded directory is not found, the directory will be created} if (!file.isempty ()) {try {bufferedoutputstream out = new Bufferedoutputstream (New F Ileoutputstream (Tempfile)); Start Uploading Out.write (File.getbytes ()); Out.flush (); Out.close (); } catch (FileNotFoundException e) {e.printstacktrace (); Result.put ("msg", "Upload file generated error," + e.getmessage ()); Result.put ("result", false); } catch (IOException e) {...} } return result;
Spring-boot Background Full code:
1 ImportJava.io.BufferedOutputStream;2 ImportJava.io.File;3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.text.SimpleDateFormat;7 Importjava.util.Date;8 ImportJava.util.HashMap;9 ImportJava.util.Map;Ten One Importjavax.servlet.http.HttpServletRequest; A - Importorg.springframework.beans.factory.annotation.Autowired; - ImportOrg.springframework.beans.factory.annotation.Value; the Importorg.springframework.web.bind.annotation.RequestMapping; - ImportOrg.springframework.web.bind.annotation.RequestMethod; - ImportOrg.springframework.web.bind.annotation.RequestParam; - ImportOrg.springframework.web.bind.annotation.ResponseBody; + ImportOrg.springframework.web.bind.annotation.RestController; - ImportOrg.springframework.web.multipart.MultipartFile; + A @RestController at@RequestMapping ("/upload") - Public classUploadfilecontroller { - //Storing Files -@RequestMapping (value = "/uploadfile", method={requestmethod.post}) - PublicMap UploadFile (@RequestParam ("File") Multipartfile file,httpservletrequest req) { -Map result =NewHashmap<>(); inSimpleDateFormat DF =NewSimpleDateFormat ("YyyyMMdd");//Set Date format -String Datedir = Df.format (NewDate ());//new Date () to get the current system time toFile tempfile =NewFile (Filedir + Datedir +File.separator ++file.getoriginalfilename ()); - the if(!Tempfile.getparentfile (). exists ()) { * tempfile.getparentfile (). Mkdirs (); $ }Panax Notoginseng if(!File.isempty ()) { - Try { theBufferedoutputstream out =NewBufferedoutputstream (NewFileOutputStream (tempfile)); + //"d:/" +file.getoriginalfilename () specify directory A Out.write (File.getbytes ()); the Out.flush (); + out.close (); -}Catch(FileNotFoundException e) { $ e.printstacktrace (); $Result.put ("msg", "Upload file produces error," +e.getmessage ()); -Result.put ("Result",false); -}Catch(IOException e) { the e.printstacktrace (); -Result.put ("msg", "Upload file produces error," +e.getmessage ());WuyiResult.put ("Result",false); the } -Result.put ("msg", "Upload succeeded"); WuResult.put ("Result",true); -}Else { AboutResult.put ("msg", "Upload file is empty"); $Result.put ("Result",false); - } - returnresult; - } A}
View Code
This code can be put directly into the spring-boot to run,
To upload a background configuration from multiple files :
JQuery file Upload is a support multi-file upload plugin, this time if you continue to invoke the above single-file upload interface directly to the front desk error, of course, there are other reasons.
Note that the following points are good:
1. Because it is multi-file, so the upload is an array file, so need to use: multipartfile[] Multipartfiles
2. Because of the input box of the jquery File upload: <input id= "FileUpload" type= "File" name= "files[" multiple>
Name= "files[]" this name needs to be equal to the value of @RequestParam (value = "files[]") so that the file can be uploaded.
Full Background code
1 ImportJava.io.File;2 ImportJava.util.Map;3 4 Importjavax.servlet.http.HttpServletRequest;5 ImportJavax.servlet.http.HttpServletResponse;6 7 Importorg.springframework.web.bind.annotation.RequestMapping;8 ImportOrg.springframework.web.bind.annotation.RequestMethod;9 ImportOrg.springframework.web.bind.annotation.RequestParam;Ten ImportOrg.springframework.web.bind.annotation.RestController; One ImportOrg.springframework.web.multipart.MultipartFile; A - @RestController -@RequestMapping ("/upload") the Public classUploadfilecontroller { - //Storing Files -@RequestMapping (value = "/uploadfile", method={requestmethod.post}) - PublicMap UploadFile (@RequestParam (value = "files[")) multipartfile[] multipartfiles,httpservletrequest req) { +Map result =NewHashmap<>(); -String savepath = ' file directory '; + if(NULL! = multipartfiles && multipartfiles.length > 0){ A //Traverse and save a file at for(Multipartfile file:multipartfiles) { -File.transferto (NewFile (Savepath +file.getoriginalfilename ())); - } - } -Result.put ("msg", "Upload succeeded"); -Result.put ("Result",true); in returnresult; - } to}
View Code
Finally, the use of the front-end jquery File upload:
Under the static directory under the Spring-boot project:
That's it, it's done. So it's very convenient to use.
Spring-boot's jquery File upload background configuration method