title: Validation for File Size Limit Multipart using Spring Java
body: <p>Can someone help me? I have a issue in validation of file size limit in Rest controller using multipart but it does not work. I already get the size of multipart and add some if statement to message the exceed limit of the file but its not working.</p>
  <pre><code>    @RequestMapping(value = &quot;/importCsvFile&quot;, method = RequestMethod.POST)
      public ResponseEntity&lt;?&gt; importCsvFile(MultipartFile inputFile) throws Exception {
  
          if (inputFile == null || inputFile.isEmpty()) {
  
              return new ResponseEntity&lt;&gt;(&quot;Please insert a CSV file.&quot;, HttpStatus.NOT_FOUND);
  
          }
          if (!inputFile.getContentType().equals(&quot;application/vnd.ms-excel&quot;)) {
  
              return new ResponseEntity&lt;&gt;(&quot;Upload CSV file only.&quot;, HttpStatus.NOT_ACCEPTABLE);
          }
          if (inputFile.getSize() &gt; 1048576) { **====&gt; File size limit**
  
              return new ResponseEntity&lt;&gt;(&quot;File too Large.&quot;, HttpStatus.NOT_ACCEPTABLE);
          } 
          else {
  
              List&lt;Object&gt; items = objectSrvc.importCsvFile(inputFile.getInputStream());
  
              if (items == null) {
                  return new ResponseEntity&lt;List&lt;Object&gt;&gt;(HttpStatus.NO_CONTENT);
              }
              return new ResponseEntity&lt;List&lt;Object&gt;&gt;(items, HttpStatus.OK);
          }
      }
  </code></pre>