File upload of spring MVC

Time:2020-1-18

Summary
File upload is a common function in Java Web projects. If the purpose of file upload is not achieved, the following steps are required:
1. The method property of the form must be set to post.
2. The enctype attribute of the form must be set to multipart / form data (the browser will use binary flow to process the form data).
3. Add Commons fileUpload dependency in POM file.
4. Configure multipartresolver in the configuration file of spring MVC.
File upload example
The sample program is still completed in the previous spring MVCP project project (previous link: https://www.jianshu.com/p/fde4557c527c).
Background image of file upload page
Create the folder “images” in the webapp directory of the project, and put a background picture named “backimage. JPG”:

backImage

POM file configuration
Add Commons fileUpload dependency in POM file of the project:

1 
  2     commons-fileupload
  3     commons-fileupload
  4     1.4
  5

Create JSP file
Create the uploadfile.jsp file in the webapp directory of the project, and write the following program:

1 
  2 
  3 
  4     
  5     uploadfile
  6     
  7         body{
  8             background-image: url(images/backImage.jpg);
  9             background-size:cover;
 10         }
 11     
 12 
 13 
 14 
 15 file upload page
 16 
 17 
 18     
 19         
 20 file information:
 21             
 22         
 23         
 24 select File:
 25             
 26         
 27         
 28             
 29         
 30     
 31 
 32 
 33 
 34

Create controller class
Create the uploadfilecontroller class file in the package com.snow.dcl.controller, and write the following program:

  1 @Controller
  2 public class UploadFileController {
  3     @RequestMapping(value = "/upload", method = RequestMethod.POST)
  4     @ResponseBody
  5     public String upload(HttpServletRequest request, @RequestParam("information") String information, @RequestParam("file") MultipartFile file) throws IOException {
  6         if (!file.isEmpty()) {
  7             String path = request.getServletContext().getRealPath("file");
  8             String fileName = file.getOriginalFilename();
  9             File uploadFile = new File(path, fileName);
 10             if (!uploadFile.getParentFile().exists()) {
 11                 uploadFile.getParentFile().mkdirs();
 12             }
 13             file.transferTo(new File(path + File.separator + fileName));
 14             return "Success!";
 15         } else {
 16             return "No File!";
 17         }
 18     }
 19 }

The @ ResponseBody annotation is used in the controller program to return “file upload succeeded” to the front end And “uploaded file not added!” Otherwise, the return view JSP file is configured in spring MVC configuration file springmvc-servlet.xml.
Spring mvc-servlet.xml file configuration
Configure the multipartresolver in the springmvc-servlet.xml file. The added content of the configuration file is as follows:

1 
  2 
  7 
  8     
  9     
 10 
 11     
 12     
 13 
 14     
 15     
 16         
 17             /WEB-INF/content/
 18         
 19         
 20             .jsp
 21         
 22     
 23 
 24     
 25         
 26         
 27             20971520
 28         
 29         
 30         
 31             UTF-8
 32         
 33     
 34 
 35 
 36

Be careful:
1. When configuring multipartresolver, the MVC namespace is also added.
2. addConfigure to process static resources such as background images.
3. addConfiguration must be addedOtherwise, the @ controller annotation cannot be parsed. As a result, there is no matching processing request class at the time of request. Go toThat is, the default servlet processes the static resources (such as picture resources) because there is no corresponding controller, it will be processed by the default servlet.
test
Start Tomcat server. After starting, open the browser and input: http: / / localhost: 8080 / uploadfile.jsp. The access is successful.
Spring MVC上传01
Without adding any documents, submit directly:
Spring MVC上传03
To add a test TXT file:
Spring MVC上传02
Submission:
Spring MVC上传04
View the project directory. The file directory will be generated in the project directory of the target directory. The uploaded files are in the file directory:
Spring MVC上传05
At this point, the file upload function is recorded.

Recommended Today

PHP realizes UnionPay business H5 payment

UnionPay business H5 payment interface document: document address 1: H5 payment interface address: 1: Alipay payment Test address: http://58.247.0.18:29015/v1/netpay/trade/h5-pay Official address: https://api-mop.chinaums.com/ 2: UnionPay payment Test address: http://58.247.0.18:29015/v1/netpay/uac/order Official address: https://api-mop.chinaums.com/ 2: Basic parameters required by the interface The interface uses get parameters. After the interface parameters are directly put into the interface address, the […]