title: Validate uploaded file for jpg and png and save to static folder
body: <p>I am using spring boot and I want to upload an image via a form.</p>
  
  <p>It works, but there are 2 problems:</p>
  
  <p>1) I do not know how to validate the file to make sure it is either <code>jpg</code> or <code>png</code></p>
  
  <p>2) I can upload only to the <code>root</code> folder, not to the <code>static</code> folder, I do not know how to set the correct path to the <code>resources/static</code> folder</p>
  
  <pre><code>@PostMapping("/add")
      public String add(@Valid Product product, @RequestParam MultipartFile file, BindingResult bindingResult,
              RedirectAttributes redirectAttributes, Model model) {
  ...
  
  try {
              byte[] bytes = file.getBytes(); // how to validate this?
              Path path = Paths.get( file.getOriginalFilename()); // how to set path here?
              Files.write(path, bytes);
  
              redirectAttributes.addFlashAttribute("message",
                      "You successfully uploaded '" + file.getOriginalFilename() + "'");
  
          } catch (IOException e) {
              System.out.println("!!!!!!!!!!!!! Image uploaded problem !!!!!!!!!!!!!!");
              e.printStackTrace();
          }
  
  ...
  }
  </code></pre>