Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

Spring MultipartFile validation and conversion

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...