2
votes

i've a spring MVC application and when i try to upload a multipart file, a null value is passed to the controller. all the other text parameters are passed properly only the file input is passed as null. I've included the MultipartResolver bean and commons-io plus commons-fileupload dependencies in my project. i've checked it's passed in the browser's request but it's not bound in the modelAttribute.

here is a snippet code from my view

<form:form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/profile/secure/saveIdentity" commandName="profileModel">

    <span><b>Upload your passport photo</b></span>
    <form:input  path="passportPhotograph" type="file"  id="passportPhoto"/> 
</form:form>

and here is a snippet from my controller method

@RequestMapping(value = "/secure/saveIdentity", method = RequestMethod.POST)
public ModelAndView saveIdentity(@ModelAttribute("profileModel") @Valid ProfileModel profileModel,HttpServletRequest request){

    MultipartFile photo = profileModel.getPassportPhotograph();
    if(photo != null){ do something.... }
}

here is my ProfileModel.java class snippet

public class ProfileModel{

    private MultipartFile passportPhotograph;

    public MultipartFile getPassportPhotograph() {
        return passportPhotograph;
    }

    public void setPassportPhotograph(MultipartFile passportPhotograph) {
        this.passportPhotograph = passportPhotograph;
    }
    ............
}

And in my dispatcher-servlet file I have declared MultipartResolver bean:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="99999999999"/>
</bean>

and finally in my build.gradle file i added these dependencies

compile('commons-io:commons-io:2.0.1')
compile('commons-fileupload:commons-fileupload:1.3.1')

after all this it's passing null to my controller even though it's included in the HttpServletRequest. what should i do to fix this. Thanks in advance for you help.

4
just for a test, remoe the @Valid annotation and let me know is change the result - cralfaro
@cralfaro still the same results after removing Valid annotation. - Kirubel Belete
I added some extra information to verify where could be the problem. I reduced the code to the minimum test to check if we can upload a single file, no forms. Could do you check if works? - cralfaro

4 Answers

1
votes

you need to use @RequestMapping(value = "/secure/saveIdentity", method = RequestMethod.POST, headers = ("content-type=multipart/*"), produces = "application/json", consumes = "image/*").

1
votes

Solved my issue by using the StandardServletMultipartResolver which comes with the spring web framework. But you have to use Servlet version 3.0+ for this to work.

0
votes

This sample code. point is input tag name binding file in model. maybe you need library json and file. ex) commons-io, commons-fileupload and jackson, gson use to parsing in controller.

HTML

<form:form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/profile/secure/saveIdentity">

    <span><b>Upload your passport photo</b></span>
    <form:input type="file"  name="file"/> 
</form:form>

Controller

@RequestMapping(value = "test", method = RequestMethod.POST)
public String testFormData(FileAndContentModel model) {
   // break point and check model.

   return "success";
}

Model

public class FileAndContentModel {

    private MultipartFile file;

    public FileAndContentModel () {
    }

     // getter, setter


}
0
votes

This is not the solution, I just type here because is easier to read than a comment. Please try the minimum requeriment, just upload a file and verify if works.

Form:

<form:form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/profile/secure/saveIdentity">
    <span><b>Upload your passport photo</b></span>
    <input  name="file" type="file"  id="passportPhoto"/> 
</form:form>

Controller:

@RequestMapping(value = "/secure/saveIdentity", method = RequestMethod.POST)
public void saveIdentity(@RequestParam("file") MultipartFile file) {
    //please verify here if the file is null