org.springframework.web.multipart.MultipartException:当前请求不是多部分请求

时间:2013-02-21 17:01:03

标签: spring-mvc multipartform-data

我正在尝试向服务器发送多部分请求,但我收到以下异常    HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.web.multipart.MultipartException:当前请求不是多部分请求

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-body">
        <form id="imageuploadForm" action="<c:url value="/members/profileimageupload" />" method="POST" enctype="multipart/form-data">
            <div style="width:40%; float:left">
                <div class="fileupload fileupload-new" data-provides="fileupload">
                    <div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div>
                    <div>
                        <span class="btn btn-file">
                            <span class="fileupload-new">Select image</span>
                            <span class="fileupload-exists">Change</span>
                            <input id="imageFile" name="imageFile" type="file" />
                        </span>
                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                    </div>
                </div>
            </div>
            <div style="width:40%; float:right" >
                <div class="progress">
                    <div class="bar" style="width: 60%;"></div>
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
       <button class="btn btn-success" id="submit">Upload</button>
       <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true" >Close</button>
    </div>
 </div>

我发送请求的ajax调用。

$(function() {
    //twitter bootstrap script
    $("button#submit").click(function(){
        var $form = $("#imageuploadForm");
        var type = $form.attr('method');
        var url =  $form.attr('action');
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize(),

            success: function(msg){
                $("#form-content").modal('hide');
            },

            error: function(){
            }
        });
    });
});

我应该处理请求的控制器

@RequestMapping(value="/profileimageupload",method= RequestMethod.POST)
    public void uploadProfileImage(@RequestParam(value="imageFile") final MultipartFile file) throws NumberFormatException, IOException{
    ////
}            

我有多部分文件的以下网页配置

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    /**
    * Supports FileUploads.
    */
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(500000000);
        return multipartResolver;
    }
}

3 个答案:

答案 0 :(得分:28)

检查是否在 Spring-Servlet.xml 中添加了 CommonsMultipartResolver

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

然后,   将enctype添加到表单中的 multipart / form-data

<form id="fileupload" method="post" enctype="multipart/form-data">

最后在Controller中,    请求&gt; MultipartHttpServletRequest

 @RequestMapping(value = "/profileimageupload", method = RequestMethod.POST)
public ModelAndView uploadProfileImage(MultipartHttpServletRequest request) {}

依赖

  
      
  1. 公地fileupload.jar
  2.   
  3. 公地io.jar
  4.   

答案 1 :(得分:11)

请使用“标题”重试:

@RequestMapping(value = "/profileimageupload", headers = "content-type=multipart/*", method = RequestMethod.POST)

和ajax调用如下:How can I upload files asynchronously?

我希望这对你有帮助!

答案 2 :(得分:-1)

似乎您的代码表单格式不正确,而且所有内容都在

之后
'action="<c:url value="/members/profileimageupload" />"' 

被忽略了。 尝试这样写,看看它是否有效:

action="<c:url value='/members/profileimageupload' />"