36

我正在尝试向服务器发送多部分请求,但我收到以下异常 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{
    ////
}            

我对多部分文件有以下 Web 配置

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

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

3 回答 3

31

检查是否在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 中,Request > MultipartHttpServletRequest

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

依赖项

  1. commons-fileupload.jar
  2. commons-io.jar
于 2014-07-28T06:19:32.527 回答
12

再试一次“标题”:

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

和 ajax 调用如下:如何异步上传文件?

我希望这对你有帮助!

于 2013-08-17T19:29:42.433 回答
-1

似乎您的标签表单格式不正确,之后的所有内容

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

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

action="<c:url value='/members/profileimageupload' />"
于 2013-02-21T17:13:32.190 回答