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
1.4k views
in Technique[技术] by (71.8m points)

spring mvc - org.springframework.web.multipart.MultipartException: The current request is not a multipart request

I am trying to send a multipart request to the server but i am getting the following exception HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request

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

my ajax call which is sending the request.

$(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(){
            }
        });
    });
});

my controller which should handle the request

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

I had the following web configuration for multipart file

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    /**
    * Supports FileUploads.
    */
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(500000000);
        return multipartResolver;
    }
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Check whether you have added CommonsMultipartResolver in Spring-Servlet.xml.

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

Then, add the enctype to multipart/form-data in your form

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

Finally in Controller, Request > MultipartHttpServletRequest

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

Dependencies

  1. commons-fileupload.jar
  2. commons-io.jar

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

...