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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…