Show the effect first

You need two jar packages commons-fileupload-1.3.2.jar commons-io-2.5.jar

  1. Start by adding a new paragraph to web.xml
<servlet-mapping> 
	<servlet-name>default</servlet-name> 
	<url-pattern>*.jpg</url-pattern>
	<url-pattern>*.png</url-pattern> 
</servlet-mapping> 
Copy the code

JPG, *.png is allowed.

Why did you add this paragraph? Since the servlet of SpringMVC is configured with a path of “/”, static resources are not accessible by default, so this section is added to allow JPG access. And must precede the Servlet of SpringMVC. If you configure spring-MVC using the /*.do path, you won’t have this problem

  1. Add the following code to springMVC.xml to open up support for upload functionality. Npe is not added!!
<! -- id must be multipartResolver; 
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
Copy the code
  1. Upload the main page code
<body> 
	<form action="uploadImage" method="post" enctype="multipart/form-data"<input type="file" name="image" accept="image/*" /><br> 
	<input type="submit" value="Upload"> 
	</form> 
</body>
Copy the code

Upload page, note that two attributes of the form must be providedmethod=”post”Enctype =”multipart/form-dataAccept =”image/*” means that only images can be uploaded.< input type=”file” name=”image” Accept =”image/“/> This image, we’re going to use this image

  1. Prepare a utility class, UploadedImageFile

Image < input type=”file” name=”image” accept=”image/*” /

public class UploadedImageFile { 
	MultipartFile image; 
	public MultipartFile getImage(a) {
		return image; 
	}
	public void setImage(MultipartFile image) { 
		this.image = image; }}Copy the code
  1. Create the UploadController UploadController
@Controller 
public class UploadController { 
	@RequestMapping("/uploadImage") 
	public ModelAndView upload(HttpServletRequest request, UploadedImageFile file) throws IllegalStateException, IOException { 
		// Get a random file name
		/ / RandomStringUtils import the jar package location at https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core/2.2.1
		String name = RandomStringUtils.randomAlphanumeric(10);
		// Get the original file name (including the file type)
		String fileName = file.getImage().getOriginalFilename(); 
		// Intercepts the file type
		int indexdot = fileName.indexOf("."); 
		String suffix = fileName.substring(indexdot); 
		// Create a file and name it
		String newFileName = name + suffix; 
		// Create a File object and set the location and the name of the File to be saved
		File newFile = new 	File(request.getServletContext().getRealPath("/image"), newFileName); // Call the getParentFile method of newFile, return the instance of its parent directory object, and then call.mkdirs()(called by the parent directory instance) to create the folder.
		newFile.getParentFile().mkdirs(); 
		// Copy the file and write the image to disk
		file.getImage().transferTo(newFile); ModelAndView mav = new ModelAndView("showUploadedFile"); 
		mav.addObject("imageName", newFileName); returnmav; }}Copy the code
  1. Finally, add the return interface
<body> <img alt="" src="image/${imageName }"> </body>
Copy the code

Summary: 1. Use RandomStringUtils this class seems to be a long time, only the old version will support, I useXwork-core2.21 download address2. The image upload address is actually in the image folder under the project where you configured Tomcat’s server. XML configuration path. Note that it is not uploaded to a project