基于Spring REST的Apache Common文件上传不起作用

时间:2013-12-23 14:01:09

标签: java spring multipartform-data

我想上传多个文件。

但它不起作用..

我的来源。

的pom.xml

<!-- commons-io (fileupload dependency) -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.4</version>
</dependency>
<!-- fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.2</version>
</dependency>

的web.xml

<filter>
    <filter-name>multipartFilter</filter-name>
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
    <init-param>
        <param-name>multipartResolverBeanName</param-name>
        <param-value>filterMultipartResolver</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>multipartFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

根context.xml中

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

jsp文件(方法是POST或PUT。但两者都不起作用)

<form:form commandName="community" action="${CONTEXT }/community/${community.no}" method="${method}" enctype="multipart/form-data">
    <img src="" name="titleImage" id="titleImage"/>
    <br/>
    <input type="file" name="titleImageFile"/>
    <br/>
    title
    <form:input path="title" id="title" title="title"/><form:errors path="title"/>
    <br/>
    <form:textarea path="content" rows="5" cols="30"/>
    <br/>
    <img src="" name="contentImage" id="contentImage"/>
    <br/>
    <input type="file" name="contentImageFile"/>
    <br/>
    <input type="submit" value="ok" onclick="showProgressBar()"/>
</form:form>

控制器

@Secured("ROLE_ADMIN")
@RequestMapping(value = "/{no}", method=RequestMethod.POST)
public String insert(HttpServletRequest request
        , @Valid Community community
        , BindingResult bindingResult
        , SessionStatus sessionStatus
        , ModelMap model)
{
    System.out.println("IN POST>>>@@@");
    System.out.println("IN POST>>>@@@");
    System.out.println("IN POST>>>@@@");
    if(bindingResult.hasErrors())
    {
        return showWriteForm(model);
    }
    Map map = new HashMap();
    map.put("community", community);
    System.out.println("isMultipart?::"+ServletFileUpload.isMultipartContent(request));
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload fileUpload = new ServletFileUpload(factory);
    fileUpload.setSizeMax(-1);

    List<FileItem> items = null;
    try {
        items = fileUpload.parseRequest(request);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("items size :: "+items.size());
    communityService.insert(map);
    return "redirect:/community/"+community.getNo();
}

为什么物品尺寸为0?

请帮助...

我正在使用RESTFUL设计,Spring MVC,Spring-Security,Commons-io,Commons-fileupload,jstl,Spring Form标签,JSTL ......出了什么问题?

1 个答案:

答案 0 :(得分:0)

如果您想直接使用该请求,则必须使用MultipartHttpServletRequest。但我建议使用MultipartFile

public String insert(@Valid Community community,
                     @RequestParam MultipartFile titleImageFile,
                     @RequestParam MultipartFile contentImageFile