Spring upload form optional with optional file

We are creating a profile page with a form that optionally has a profile pic on it. We are using Spring 3.2

Here is the form: –

Here is the controller method: –

Here is the Model

It works fine if a file is submitted on the form, but there are errors in the BindingResult variable if the form is submitted without a file.

Here is the error: –

======================

It turns out it was the jQuery Form plugin which was sending an empty string instead of what spring expects – nothing to be sent.

I solved the problem using a before submit to remove the fileData value if it wasn’t populated like so: –

I hope this helps some googlers with the same problem.

来自:http://stackoverflow.com/questions/14771446/spring-upload-form-optional-with-optional-file