Spring uploaded Multipart file is NULL

2k Views Asked by At

I made a simple form to upload files. The problem is this form is giving me a null instead of a file every time I try to upload it.

That form looks like this:

                <sf:form method="post"
                         action="${pageContext.request.contextPath}/admin/uploadPhoto/${photo.product_id}"
                         commandName="photo"
                         enctype="multipart/form-data"
                         id="photoForm">

                    <div class="form-group">
                        <span>Photo:</span>
                        <sf:errors path="file"
                                   cssClass="alert-danger"/>
                        <sf:input cssClass="form-control"
                                  type="file"
                                  path="file"/>
                    </div>
                    <%--other inputs--%>

                    <div>
                        <input type="submit"
                               value="Upload"
                               class="btn btn-primary"/>
                    </div>
                </sf:form>

Here's part of method in controller mapping the action in form:

@RequestMapping(value = "/admin/uploadPhoto/{products_id}", method = RequestMethod.POST)
public String uploadPhoto(Model model, 
        @Valid Photo photo, BindingResult bindingResult, 
        @PathVariable int products_id) {

    MultipartFile file = photo.getFile();   //At this point I see the file is null
    //the rest of code

Photo class has following attributes (with getters and setters):

private int id;
@NotNull
private String path;
private String description;
private int product_id;
private boolean main;

private MultipartFile file;

And in dispatcher-servlet I have declared MultipartResolver bean:

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
</bean>

I have also added these dependecies to my pom.xml:

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.2</version>
    </dependency>
0

There are 0 best solutions below