Java file upload (single file and multi file)
Java file upload (single file and multi file)
1、 Brief description
In a Java Web project, the file upload function is almost essential. I often encounter it in project development, and I haven't dealt with it much before. Today I have time to learn this knowledge, so I take notes of the knowledge of single file and multi file upload in spring MVC.
2、 Single file upload
1. Page
Here, take a simple form submission as an example. For file upload, you need to set the form submission method to post and the enctype value to "multipart / form data".
2. Controller
In the processing method of the controller, the multipartfile object is used as a parameter to receive the file uploaded from the front end. Please see the code Notes for details.
3、springmvc. XML configuration
To receive the files uploaded from the front end using the multipartfile object, you also need to configure the following in the spring MVC configuration file:
The property value configuration under commonsmultipartresolver in the above configuration file is not necessary, and you can not write it all. Here you can upload a single file. Let's take a look at multiple file uploads.
3、 Multi file upload
In fact, multi file upload is also very simple. Single file upload uses the multipartfile object as a parameter in the controller's processing method to receive the files uploaded from the front end, while multi file upload uses the multipartfile object array to receive.
1. Page
There are several input tags of file type with the same name value in this page, and the pages uploaded by other documentary files are no worse.
2. Controller
The processing method in the controller uses the multipartfile [] array as the receiving parameter, which cannot be used directly. The parameters need to be corrected. Please see the code Notes for details.
Similarly, using multipart file array to receive multiple files uploaded from the front end also needs to be configured in the spring MVC configuration file. The specific configuration is the same as that of the spring MVC uploaded from the above single file The XML configuration is not bad. Just copy it directly. In this way, you can upload multiple files.
4、 Synthesis of multiple file upload scenarios
Of course, in project development, the scenario may not be so simple, The above multi file upload is to upload files together after selecting them (i.e. multiple input tags with the same name). What if only one input tag in my project can upload multiple files at one time? Or whether there should be multiple file upload selected one by one, multiple file upload selected at one time, and single file upload in a page? No problem, multipartfile [] Take all, the code is also very easy. Let's go directly to the code below.
1. Page
The "multiple file upload by selecting multiple files at a time" here is just adding the multiple attribute to the input tag.
2. Controller
Multipartfile [] is so powerful that the logical processing is the same regardless of single or multiple files. Therefore, it is recommended to use multipartfile [] as the file receiving parameter in project development.
5、 Expand
1. Some common methods of multipartfile class:
2. Property resolution of commonsmultipartresolver
6、 Attention
If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!