Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:56+00:00 2026-05-25T10:00:56+00:00

I currently have a Spring MVC controller that takes a MultipartFile @RequestMapping(method = RequestMethod.POST)

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:00:57+00:00Added an answer on May 25, 2026 at 10:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have controller method in my MVC application that I am calling using the
currently I have the following code: String select = qry.substring(select .length(),qry2.indexOf( from )); String[]
Problem, there's no method: bool ChangePassword(string newPassword); You have to know the current password
I currently have an MS Access application that connects to a PostgreSQL database via
I have a calendar that I generate. Currently it makes the entire month and
I have created an MVC application that dynamically routes actions and controllers based on
I have an ASP.NET MVC 2 application with a custom StructureMap controller factory to
I have a ASP.net MVC application that gets marker coordinates from a database (I
I have written a VERY simple MVC application which just displays a single string
I have this code: Dim pathString As String = HttpContext.Current.Request.MapPath(Banking.mdb) Dim odbconBanking As New

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.