Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

file upload - Spring 3.0 FileUpload only with POST?

I am trying to upload a File with one parameter using spring 3.

This is my controller method which should enable this service:

@RequestMapping(value="/{id}", method = RequestMethod.PUT, headers="content-type=multipart/form-data")
public ResponseEntity<String> uploadImageWithJsonParamater(@PathVariable("id") Long id, @RequestParam String json, @RequestParam MultipartFile customerSignFile) {
    //...
}

The problem is, that the server cannot dispatch to this method: MissingServletRequestParameterException: Required String parameter 'json' is not present

If I change the RequestMethod from PUT to POST, everything is fine. So does anybody know the problem?

It seems that it isn't allowed to transmit form-data via PUT.

I debugged a little bit and the following method returns false in the PUT case but true in the POST case:

public boolean isMultipart(HttpServletRequest request) {
    return (request != null && ServletFileUpload.isMultipartContent(request));
}

I would appreciate any help!

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can accomplish this using spring's HiddenHttpMethodFilter, but you will need to ensure that you put a Spring MultipartFilter before the HiddenHttpMethodFilter in your web.xml filter chain.

For example: In your 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>
    <servlet-name>/*</servlet-name>
</filter-mapping>
<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <servlet-name>/*</servlet-name>
</filter-mapping>

Then in your spring-config.xml add a reference to CommonsMultipartResolver:

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

Note that, if you don't add the spring-config.xml entry your MultipartFilter will default to using a MultipartResolver that uses servlet spec 3.0 implementation and will throw error like: NoSuchMethodError HttpServletRequest.getParts() if you're not using 3.0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...