Learning a framework and learning file upload
When using SPRINGMVC for system implementation, the SPRINGMVC default parser is not added to the file upload parsing, which can facilitate our implementation of their own file upload. However, if you want to use Springmvc file upload parser to handle file uploads, you need to add SPRINGMVC applicationcontext inside the spring to provide the Multipartresolver declaration. Then, each time the client makes a request, SPRINGMVC checks to see if it contains multimedia information, and if it does, it uses Multipartresolver to parse it. SPRINGMVC uses a multiparthttpservletrequest that supports file handling to wrap the current httpservletrequest, You can then use Multiparthttpservletrequest to process the file.
1. Introducing a Dependency Package
File upload requires additional ingestion of packages, respectively
- Commons-fileupload-1.3.1.jar
- Commons-io-2.4.jar
2. Configure JSP pages
<%@ page contenttype="Text/html;charset=utf-8" language="java" %><% @taglib prefix="SF" uri="Http://www.springframework.org/tags/form "%><html><head> <meta charset="Utf-8"> <title>User Login</title></head><body> <%--file Upload words need enctype="Multipart/form-data"--%> <sf:form modelattribute="User" method="POST" enctype ="Multipart/form-data">User name:<sf:input path="username"/><sf:errors path="username"/> <br>Password:<sf:input path="password"/><sf:errors path="password"/> <br>Nickname:<sf:input path="nickname"/><sf:errors path="nickname"/> <br> <%--here to set file upload--%>File:<input type="file" name="file"> <input type="Submit" value="Submit"> </sf:form></body></html>
3. Configure the appropriate controller
@RequestMapping(Value ="/login", method = Requestmethod.post) PublicStringLogin(@Validated user User, Bindingresult br, Model model,@Requestparam("File") Multipartfile file) {if(Br.haserrors ()) {return "User/login"; }//Get variable name---file type---filenameSystem.out.println (File.getname () +"---"+file.getcontenttype () +"---"+file.getoriginalfilename ());Try{if(!file.isempty ()) {//Copy files using STREAMSAPI modeStreams.copy (File.getinputstream (),NewFileOutputStream ("e:/temp/"+file.getoriginalfilename ()),true); } }Catch(IOException e) {System.out.println ("File upload failed"); E.printstacktrace (); } System.out.println (User.tostring ());return "User/login"; }
This completes the upload of a single file.
4. Multi-File Upload
Multi-File upload is simple, just need to change the parameters to the array can be
@RequestMapping(Value ="/login", method = Requestmethod.post) PublicStringLogin(@Validated user User, Bindingresult br, Model model,@Requestparam("File") multipartfile[] file) {if(Br.haserrors ()) {return "User/login"; }//The file is traversed here for(Multipartfile mul:file) {//Get variable name---file type---filenameSystem.out.println (Mul.getname () +"---"+mul.getcontenttype () +"---"+mul.getoriginalfilename ());Try{if(!mul.isempty ()) {streams.copy (Mul.getinputstream (),NewFileOutputStream ("e:/temp/"+mul.getoriginalfilename ()),true); } }Catch(IOException e) {System.out.println ("File upload failed"); E.printstacktrace (); }} System.out.println (User.tostring ());return "User/login"; }
SPRINGMVC Study Record (iv)--File upload