Principle analysis of spring boot file upload

First of all, we need to know what spring boot is. To be brief here, spring boot can be regarded as a framework in a framework -- > it integrates various frameworks, such as security, JPA, data, cloud, etc. it does not need to care about configuration and can quickly start development. If you are interested, you can understand the principle of automatic configuration implementation. In essence, it is the conditional configuration implementation of spring 4.0, Throw down the notes and you'll see.

The principle of spring boot file upload is actually spring MVC, because this part of the work is done by spring MVC rather than spring boot. Then, how does spring MVC handle the process of file upload?

Figure:

First, the project starts the relevant configuration, and then when the second step above is executed, the dispatcher servlet will find the bean with ID of multipartresolver. In the configuration, it can be seen that the bean points to commonsmorpartresolve, which implements the multipartresolver interface.

The fourth step is to judge whether the multipart file, i.e. ismultipart method, returns true: the multipartresolver method will be called. Passing HttpServletRequest will return a multiparthttpservletrequest object, and then the dispatcher servlet will process it to the controller layer; Return false: it will be ignored and continue to pass HttpServletRequest.

In MVC, the configuration file webapplicationcontext XML is configured as follows:

The spring boot has been automatically configured. Just use it directly. There's no problem making a test. There is a default upload limit, but we still do some configuration in actual development,

As follows in application In properties:

Of course, you can also write configuration classes to implement them. The specific ones will not be shown.

After reading the above, you must have a general understanding. Here is the multipart parser provided by spring: multipart resolver. The above is commons multipart resolver, which is implemented based on the third party of Commons file upload, which is also in servlet3 For things before 0, after 3.0 +, there is no need to rely on a third-party library. You can use standardservletmultipartresolver, which also implements the multipartresolver interface. We can see its implementation:

Here is the latter implementation configuration class of test written earlier. You can take a brief look to understand:

summary

The above is the analysis of the spring boot file upload principle introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>