title: How to validate file type, size while uploading in Rest in Spring?
body: <p>I am trying to implement file uploading using spring and Rest. Here what I have done this far</p>
  
  <pre><code>@RestController
  @RequestMapping("/rest/upload")
  public class ProfileImageUploadController {
  
     @Autowired
     ImageValidator imageValidator;
  
     @RequestMapping(value="/{userId}/image", method=RequestMethod.POST)
     public @ResponseBody String handleFileUpload(
          @PathVariable("userId") Integer userId,
          @ModelAttribute("image") SingleImageFile image,
          BindingResult result){
  
         MultipartFile file = image.getFile();
         imageValidator.validate(file, result);
         if(!result.hasErrors()){
             String name = file.getOriginalFilename();
             try{
                 file.transferTo(new File("/home/maclein/Desktop/"+name));
                 return "You have successfully uploaded " + name + "!";
             }catch(Exception e){
                 return "You have failed to upload " + name + " =&gt; " + e.getMessage();
             }
         } else {
             return result.getFieldErrors().toString();
         }
     }
  }
  </code></pre>
  
  <p>Here is my ImageValidator</p>
  
  <pre><code>@Component
  public class ImageValidator implements Validator {
     @Override
     public boolean supports(Class&lt;?&gt; arg0) {
         // TODO Auto-generated method stub
         return false;
     }
  
     @Override
     public void validate(Object uploadedFile, Errors error) {
         MultipartFile file = (MultipartFile) uploadedFile;
  
         if(file.isEmpty() || file.getSize()==0)
             error.rejectValue("file", "Please select a file");
         if(!(file.getContentType().toLowerCase().equals("image/jpg") 
              || file.getContentType().toLowerCase().equals("image/jpeg") 
              || file.getContentType().toLowerCase().equals("image/png"))){
             error.rejectValue("file", "jpg/png file types are only supported");
         }
     }
  }
  </code></pre>
  
  <p>But while testing through postman its showing the error if file is pdf but  in a weird way. Here is the string representation of the error</p>
  
  <blockquote>
    <p>"[Field error in object 'image' on field 'file': rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@3fc04a65]; codes [jpg/png file types are only supported.image.file,jpg/png file types are only supported.file,jpg/png file types are only supported.org.springframework.web.multipart.MultipartFile,jpg/png file types are only supported]; arguments []; default message [null]]"</p>
  </blockquote>
  
  <p>I cannot understand why the error list length is 4. My motive is to show error in json if it is not validated. </p>
  
  <p>If there is any standard way to do this kind of validation ? I am new to spring and Rest. So someone please show me the way to achieve the goal.</p>