Implementation method of file upload function in spring MVC environment

preface

In the actual development process, especially in the development of web projects, the functions of file upload and download are very different, such as user avatars, product pictures, e-mail attachments and so on. In fact, the essence of file upload and download is to read and write in the form of stream, and different frameworks will more or less encapsulate file upload and download in development. Here, take file upload in spring MVC environment as an example to explain the implementation of file upload function in Spirng MVC environment. I won't say much now. Let's take a look at the detailed introduction.

1、 Client programming

Since most file uploads are submitted to the background server in the form of forms, a file upload form is required to realize the file upload function. At the same time, the form must meet the following three conditions:

Before HTML5, if you want to upload multiple files, you must use multiple < input > elements. However, in HTML5, you can upload multiple files by configuring the multiple attribute in the < input > element. Write any of the following lines of code in HTML5 to generate a button to select multiple files:

Specific code examples are as follows:

2、 Springmvc config XML configuration file configuration

When the enctype attribute of the client form is multipart / form data, the browser will process the form data in the form of binary stream, and the processing of file upload involves parsing the original HTTP request on the server. Spring MVC provides direct support for file upload, which is implemented with the plug and play multipartresolver.

Using multipartresolver in sring MVC is also very simple. You only need to use springmvc config. In the spring MVC configuration file The multipartresolver interface implementation class can be defined in XML. An example is as follows:

be careful:

Since the multipartresolver implementation class commonsmultipartresolver internally refers to the multipartresolver string to obtain the implementation class object and complete the file upload operation, the ID of the bean must be specified as multipartresolver when configuring commonsmultipartresolver. In addition, because the multipartresolver implementation class commonsmultipartresolver is implemented in spring MVC through Apache Commons fileUpload technology. Therefore, the file upload of Spirng MVC also depends on the component of Apache Commons fileUpload, that is, the jar package supporting file upload and download needs to be imported, as follows:

3、 Background file download implementation (Introduction to multipartfile interface)

After writing the above client code and configuring the file upload component, it is very easy to process the uploaded files in spring MVC. The files uploaded to the spring MVC program will be encapsulated in a multipartfile object, and then the developer can carry out subsequent related processing through the method of this object.

Write the background file upload controller class and method. The specific examples are as follows:

4、 Effect demonstration (readers can write their own projects and JSP pages)

Deploy and start the web server, and enter the address in the browser address: http://localhost:8080/webname/toFileUpload After (readers access according to their own projects and paths), the display effect of the browser is shown in the following figure.

I chose to upload 2 files above, and then click submit to complete the upload. After operation, readers can verify at the designated upload location.

So far, the file upload function code under sring MVC environment has been realized.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>