title: Get correct file size from Spring MultipartFile
body: <p>How to find correct file size from <code>org.​springframework.​web.​multipart.​MultipartFile</code></p>
  
  <pre><code>double bytes = m.getSize();
  double kilobytes = (bytes / 1024);
  double megabytes = (kilobytes / 1024);     
   System.out.println(megabytes);
  </code></pre>
  
  <p>but its not giving the correct size.</p>
  
  <p>Here <code>m.getSize()</code> return 2264855 but using this method it gives file size something 2MB but actual size in my system of file is 8MB</p>
  
  <pre><code>private HashMap add(@RequestParam( value = "files", required = false) MultipartFile[] mPartFile){
  
      if(mPartFile != null &amp;&amp; mPartFile.length&gt;0){
          for(MultipartFile m:mPartFile){
              if(!CoreServices.isSupportedFile(m.getContentType())){
                  hm.put("status", MConstants.OPERATION_FAILED);
                  hm.put("reason", "File: "+m.getOriginalFilename()+" is not supported. Upload only jpeg, png, gif image files.");
                  return hm;                
              }
              long size = m.getSize();
              if( size&gt; MConstants.MAX_ALLOWED_FILE_SIZE){
                  hm.put("status", MConstants.OPERATION_FAILED);
                  hm.put("reason", "File: "+m.getName()+" is not acceptable, its size is more than 5MB.");
                  return hm;                
              }            
          }
      }
  }
  </code></pre>
  
  <p>Here the value of <code>MAX_ALLOWED_FILE_SIZE</code> is <code>5000000</code> for 5MB limitation</p>