Spring MultipartFile validation and conversion

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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 {     /* ... */ } 

And a Validator for the file

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

And I have a form to do the uploading:

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 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:

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 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 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 imports =          converter.convert(backingForm.getMultipartFile().getInputStream());  } 

The UploadConverter is much the same as the UploadValidator:

public class UploadConverter implements Converter> {     private final Converter converter = new MyObjectConverter();      @Override     public List convert(final InputStream source) {         final List detailLines = /* ... getDetailLines */         final List importList =              new ArrayList(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) {} 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!