FileUpload is a common requirement at the presentation layer. In Spring MVC, Apache Commons FileUpload tool is used to complete FileUpload and encapsulate it, making it more convenient for developers to use. Now how do you develop it?

1 Import the common-Fileupload package

<! -- commons-fileUpload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> The < version > 1.3.1 < / version > < / dependency >Copy the code

2 Configure the file parser

<! Config file upload resolver note: Id must be configured. And name must be multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <! <property name="maxUploadSize" value="1024000"/> </bean>Copy the code

A few points to note:

  • Must be configuredCommonsMultipartResolverThe parser
  • The id of the parser must be calledmultipartResolverOtherwise, the file cannot be received successfully
  • Can be achieved bymaxUploadSizeProperty to limit file upload size

3 Design a file upload form

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> <body> <h3>SpringMVC file upload </h3> <form Action ="/upload" method="post" enctype="multipart/form-data"> Select file: <input type="text" name="memo"> <br/> <input type=" value=" upload "> </form> </body> </ HTML >Copy the code

Note the following points when uploading the form:

The encType of the form must be changed to multipart/form-data. The form submission mode must be POST, not GET

4 Write the receiving files and parameters of the controller

package com.yiidian.controller; import com.yiidian.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.UUID; */ @controller public class UploadController {/** * accept file */ @RequestMapping("/upload") public String upload(HttpServletRequest request, MultipartFile imgFile,String memo){ //1. String upload = request.getSession().getServletContext().getrealPath ("/upload"); UploadFile = new File(upload); if(! uploadFile.exists()){ uploadFile.mkdir(); } / / save the file to upload the directory / / 2. Generate random file name / / 2.1 of the original file name String oldName = imgFile. GetOriginalFilename (); String uuid = uuid.randomuuid ().tostring (); String extName = oldName.subString (oldName.lastIndexof (".")); //.jpg //2.4 final fileName String fileName = uuid+extName; Imgfile. transferTo(new File(upload+"/"+fileName)); } catch (IOException e) { e.printStackTrace(); } system.out. println(" file description: "+memo); return "success"; }}Copy the code

Note: The MultipartFile object is used to receive the file and store it in the upload directory of the project, along with the normal parameters.

5 Running Tests

Check the project target directory for files

Console output parameter content:

The source code download: https://pan.baidu.com/s/1mtuGCnLaoq0IEemviwMg

Welcome to pay attention to my public number: : a little tutorial. Get exclusive organized learning resources and daily dry goods push.

If you are interested in my series of tutorials, you can also check out my website: yiidian.com