Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;     } } 
like image 922
Saurabh Kumar Avatar asked Feb 21 '13 17:02

Saurabh Kumar


People also ask

How do you resolve current request not a multipart request?

The Best Answer is I was also facing the same issue with Postman for multipart . I fixed it by doing the following steps: Do not select Content-Type in the Headers section. In Body tab of Postman you should select form-data and select file type .

What is multipart http request?

An HTTP multipart request is an HTTP request that HTTP clients construct to send files and data over to an HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server.

What is MultipartFile?

public interface MultipartFile extends InputStreamSource. A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired ...

What is multipart request in spring?

Multipart requests consist of sending data of many different types separated by a boundary as part of a single HTTP method call. Generally, we can send complicated JSON, XML, or CSV data, as well as transfer multipart file(s) in this request. Examples of multipart files include audio or image files.


1 Answers

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
like image 139
Randula Avatar answered Oct 16 '22 17:10

Randula