Adding the ability to upload files is a bit more complex. Let’s start by adding two essential libraries:
Commons-IO and commons-fileupload. Both are in the sample directory libs for this chapter. As soon as we are at ground level, let’s add *** to our *** – servlet.xml bean with the ID “multipartResolver” in which parameter p: maxUploadSize declares the maximum size of the uploaded file in bytes. Spring will require configuration of this bean, so we can not skip this step.
Let’s move on to the processing of the form itself:
We will start by adding the enctype = “multipart / form-data” parameter to the form. It allows you to upload files (see line 11). Now, look at line 14. Here is a very important trifle: we have input here and not input form. This is not a car class field! We will put the image to be processed as a separate call parameter. Let’s now go to the controller that handles the form.
He has come up with a new parameter to the post this method. It is the “photo” parameter. Note that it is not passed by the model, only as a separate parameter.
An element from line 44-46 is just a bajer, verifying that the image is a picture. You can throw some exceptions if you do not appear to be. Lines 47-50 is a file on the disk.