Spring multipart upload

Im trying to write a controller and a form that can handle a multipart file upload and some other data passing. First i made the basic form like this:

<form:form method="POST" commandName="myForm">

then everything fine, but no multipart handling of course. Then i add the enctype part like this:

<form:form method="POST" commandName="myForm" enctype="multipart/form-data">

Then my whole form is messed up and all the attribuets gives NullPointers. Not even a simple String name attribute working. Also i added:

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

So i really got no idea whats the problem. Any comment would help a lot. Thnaks in advance.


We are using CommonsMultipartResolver in our project. It goes like this. In your applicationContext.xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="1048576000"/>
    <property name="defaultEncoding" value="UTF-8" />
</bean>

Then cast yout request to MultipartHttpServletRequest:

public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    if (!(req instanceof MultipartHttpServletRequest)) {
        error(resp, "Invalid request (multipart request expected)");
        return null;
    }
Map<String, MultipartFile> files = ((MultipartHttpServletRequest)req).getFileMap();
... do thomething with the files
链接地址: http://www.djcxy.com/p/48710.html

上一篇: RestTemplate可以消耗多部分/混合吗?

下一篇: Spring分段上传