Spring MultipartFile validation and conversion

2019-01-12 08:51发布

问题:

I currently have a Spring MVC controller that takes a MultipartFile

@RequestMapping(method = RequestMethod.POST)
public String doUpload(@RequestParam("file") final MultipartFile file) {
    /* ... */
}

The file contains csv data which will be used, one per row, to create a list of domain objects. This is working.

I have written a converter for the line data:

class MyObjectConverter implements org.springframework...Converter<String[], MyObject> {
    /* ... */
}

And a Validator for the file

class UploadFileValidator implements org.springframework.validation.Validator { 
    /* ... */
}

And I have a form to do the uploading:

<form method="post" 
    action="<@spring.url '/upload'/>" 
    enctype="multipart/form-data">
        <input id="upload" type="file" name="file"/>
        <input type="submit" id="uploadButton"/>
    </form

But what I really want to do is tie it all together so that my controller can have a method something like

@RequestMapping(method = RequestMethod.POST)
public String doUpload(
    @Valid final List<MyObject> objList, 
    final BindingResult result) { ...}

I know that the Spring MVC framework supports converters and validators, but I am failing to understand how to get them to work together.

回答1:

First I wrapped the MultipartFile in a form backing object:

public class UploadBackingForm {
    private MultipartFile multipartFile;
    /* ... getter/setter */
}

Which I then bound to my form:

<form method="post" enctype="multipart/form-data">
<@spring.bind "backingform.*"/>
<tr>
    <td><@spring.formInput 'backingform.multipartFile' '' 'file' /></td>
    <td> <button type="submit">Upload</button> </td>
</tr>
</form>

In the controller I assign a validator:

@InitBinder
public void initBinder(final DataBinder binder) {
    binder.setValidator(new UploadValidator());
}

And this is the validator:

public class UploadValidator implements Validator {
    private final Converter<String[], MyObject> converter 
        = new MyObjectConverter();

    @Override
    public boolean supports(final Class<?> clazz) {
        return UploadBackingForm.class.equals(clazz);
    }

    @Override
    public void validate(final Object target, final Errors errors) {
        final UploadBackingForm backingForm = (UploadBackingForm) target;
        final MultipartFile multipartFile = backingForm.getMultipartFile();
        final List<String[]> uploadData = /* parse file */
        for (final String[] uploadDataRow : uploadData){
            try {
                converter.convert(uploadDataRow);
            } catch (IllegalArgumentException e) {
                errors.rejectValue("multipartFile", "line.invalid", ...);
            }
        }
    }
}

The validator uses a Converter for the line-item conversion to MyObj.

The doPost method now looks like this:

@RequestMapping(method = RequestMethod.POST)
public String doUpload(
    @Valid @ModelAttribute("backingform") UploadBackingForm backingForm, 
    final BindingResult result, 
    final HttpSession session) throws IOException {

    final UploadConverter converter = new UploadConverter();
    final List<MyObj> imports = 
        converter.convert(backingForm.getMultipartFile().getInputStream());
 }

The UploadConverter is much the same as the UploadValidator:

public class UploadConverter implements Converter<InputStream, List<MyObject>> {
    private final Converter<String[], MyObject> converter = new MyObjectConverter();

    @Override
    public List<MyObject> convert(final InputStream source) {
        final List<String[]> detailLines = /* ... getDetailLines */
        final List<MyObject> importList = 
            new ArrayList<MyObject>(detailLines.size());

        for (final String[] row : detailLines) {
            importList.add(converter.convert(row));
        }
        return importList;
    }
}

The only problem is that the validation and conversion processes are much the same thing. Luckily the upload files will not be very large so the duplication of effort is not a big problem.



回答2:

You need a converter that is able to convert a MultipartFile into MyObjects, but not to convert String[] to MyObjectsContainer. - MyObjectsContainer is nothing more than a wrapper around the list of MyObjects.

But I really don't know if this converter will work, because MultipartFile is some very special parameter.

For Validation I strongly recommend JSR 303 Bean Validation annotations at MyObjectsContainer

Then you can write:

@RequestMapping(method = RequestMethod.POST)
public String doUpload(@RequestParam("file") final MyObjectsContainer container) {}