Spring上传的Multipart文件为NULL

时间:2017-01-02 21:05:22

标签: java spring file spring-mvc multipart

我制作了一个简单的表单来上传文件。问题是这个表单每次尝试上传时都会给我一个null而不是一个文件。

该表格如下:

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

以下是控制器在表单中映射操作的方法的一部分:

@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类具有以下属性(使用getter和setter):

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

private MultipartFile file;

在dispatcher-servlet中,我声明了MultipartResolver bean:

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

我还将这些依赖添加到我的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 个答案:

没有答案