---restore content starts---
File upload download When the project development most commonly used features, upload files when the form must be set up as follows:
- Set method to post
- and set the enctype to Multipart/data
This allows the browser to send the user's selected file binary data to the server
Springle MVC provides direct support for file uploads, and this support is implemented with Multipartresolver, SPRINGMVC upload relies on Apache Commons fileupload support
That requires a Commons-fileupload-xx.jar rack package,
Here's a small example of uploading first.
1. First, configure the Multipartresolver in the configuration file
<BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--Maximum upload file size in bytes (10MB) - < Propertyname= "Maxuploadsize"> <value>10485760</value> </ Property> <!--the requested encoding format must be consistent with the Pageencoding property of the JSP in order to correctly read the contents of the form, default to Iso-8859-1 - < Propertyname= "Defaultencoding"> <value>UTF-8</value> </ Property> </Bean>
2. Front Page
<Body> <H2>Spring MVC File Upload</H2> <formAction= "Upload"enctype= "Multipart/form-data"Method= "POST"> <Table> <TR> <TD>File description</TD> <TD><inputtype= "text"name= "description"/></TD> </TR> <TR> <TD>Please select a file</TD> <TD><inputtype= "File"name= "File"></TD> </TR> <TR> <TD><inputtype= "Submit"value= "Upload"></TD> </TR> </Table> </form></Body></HTML>
3. Upload Method
@RequestMapping (value= "/{formname}") Publicstring LoginForm (@PathVariable string formName) {//Dynamic Jump Page returnFormName; } //The upload file is automatically bound to the Multipartfile@RequestMapping (value= "/upload", method=requestmethod.post) PublicString Upload (httpservletrequest request, @RequestParam ("description") String description, @RequestParam ("File") Multipartfile file, model model)throwsexception{System.out.println (description); //If the file is not empty, write the upload path if(!File.isempty ()) { //Upload file pathString Path =Request.getservletcontext (). Getrealpath ("/images/"); //Upload file nameString filename =File.getoriginalfilename (); File filepath=NewFile (path,filename); //determine if the path exists, and if it does not exist, create a if(!Filepath.getparentfile (). exists ()) {System.out.println ("Path does not exist"); Filepath.getparentfile (). Mkdirs (); } System.out.println (Filepath.getparentfile ()); //Save the uploaded file to a target fileFile.transferto (NewFile (path+file.separator+filename)); return"Success"; }Else{ return"Error"; } }
Spring MVC binds the uploaded file to Multipartfile, Multipartfile provides a way to get the contents of the uploaded file, the file name, and the Transto () method to store the file in hardware, Multipartfile
The usual methods are as follows:
- Byte[] GetByte (): Get File
- String getContentType (): Gets the file MIME type, such as Image/jpeg
- InputStream getinputstream () get file stream
- String getName (). Get the name of the file component in the form
- String Getoriginalfilename () Gets the original of the uploaded file
- Long GetSize (): Get file byte size
- Boolean isEmpty (): Whether there is an upload file
- void TransferTo (File dest): Save the file to a destination file
Spring mvc file Upload download