Could not parse multihearted servlet request, nested exception - org.apache.commons.fileupload.FileUploadException

I am getting an error when downloading 10 MB. Spring csv file size using CommonsMultipartResolver library. I made the following setting in the Xml File Confi XML:

 <beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<beans:property name="maxUploadSize" value="99971520" /> <!-- 99MB -->

<!-- max size of file in memory (in bytes) -->
<beans:property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

      

Controller code:

 @RequestMapping(value="/uploadForm",method = RequestMethod.POST)
public @ResponseBody  String  uploadForm1(@ModelAttribute("admin") BillingAndRecon  billingandrecon,@RequestParam String id,BindingResult result,Principal principal,@RequestParam MultipartFile file,HttpSession session) throws ServiceException, DaoException, IllegalStateException, IOException {


    File uploadFile = null;
    String msg = "";

    if (!file.getOriginalFilename().equals("")) {

        logger.info("Before  Multipart file get path >> ");
        BillingAndReconServiceImpl asi = (BillingAndReconServiceImpl) this.billingAndReconService;// not correct!!
        String uploadDirectoryPath = asi.getUploadDirectoryPath(); // not correct!!

        uploadFile = new File( uploadDirectoryPath + file.getOriginalFilename());
        logger.info("Before  Multipart file get path uploadDirectoryPath >> "+uploadDirectoryPath);
        file.transferTo(uploadFile);
    }
}

      

Form page:

<form:form action="./uploadForm" method="post" enctype="multipart/form-data" ModelAttribute=="admin">
          <input type="file" name="file" />
          <input type="text" name="id" />
           </form:form>

      

But I don't understand what the problem is. I tried adjusting the size and setting the title as well enctype="multipart/form-data"

, but haven't solved it yet.

Below are the errors:

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:954)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:892)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

      

+5


source to share


1 answer


As requested by @Christian Maioli M, more details about the issue have been added in the comment. There are BindingResult parameters in your code that don't match the model object .

The Errors or BindingResult parameters must match the model object that binds immediately, since a method signature can have more than one model object and Spring will create a separate BindingResult instance for each, so the following example will not work

See the docs BindingResult and @ModelAttribute Wrong Order

To solve this problem, change the signature of the controller method handler to match the order of the parameters between the BindingResult and the model object, for example:

From:

@RequestMapping(value="/uploadForm",method = RequestMethod.POST)
public @ResponseBody  String  uploadForm1(@ModelAttribute("admin") BillingAndRecon  billingandrecon,@RequestParam String id,BindingResult result,Principal principal,@RequestParam MultipartFile file,HttpSession session) throws ServiceException, DaoException, IllegalStateException, IOException {

      

In order to:

@RequestMapping(value="/uploadForm",method = RequestMethod.POST)
public String  uploadForm1(
            @ModelAttribute("admin") BillingAndRecon billingandrecon, 
            BindingResult result,
            Principal principal,
            HttpSession session) throws ServiceException, DaoException, IllegalStateException, IOException {
  //do file save here
  return "some-view-name";
}

      



and in the BillingAndRecon class add multiitpart / binding fields like:

public class BillingAndRecon {
  private MultipartFile file;
  private String id;

  no-arg constructor;
  getters;
  setters;
}

      

Note. The BindingResult argument must be @ModelAttrubiute/@RequestBody

immediately after@ModelAttrubiute/@RequestBody

and the jsp form:

<form:form action="${pageContext.request.contextPath}/uploadForm"  
   method="post" 
   enctype="multipart/form-data" 
   modelAttribute="admin">
      <input type="file" name="file" />
      <form:input path="id" />
</form:form>

      

and don't forget to add an instance for the binding in your GET handler, for example:

@RequestMapping(value="/uploadForm",method = RequestMethod.GET)
public String uploadFormView(Model model){
  model.addAttribute("admin", new BillingAndRecon());
  return "your-upload-view-name";
}

      

0


source







All Articles