title: Spring Boot images uploading and serving
body: <p>I'm making new Spring Boot app and want to be able to store and serve images, I want images to be stored in applications directory:<br>
  <a href="https://i.stack.imgur.com/GolEj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GolEj.png" alt="enter image description here"></a></p>
  
  <p>this is what uploading looks like for now:</p>
  
  <pre><code>@PostMapping("/")
  @ResponseBody
  public String upload(@RequestPart String title, @RequestPart MultipartFile img) throws IOException{
      String imgName = img.getOriginalFilename();
      Post p = new Post();
      p.setTitle(title);
      p.setImageName(imgName);
      postService.add(p);
      File upl = new File("images/" + imgName);
      upl.createNewFile();
      FileOutputStream fout = new FileOutputStream(upl);
      fout.write(img.getBytes());
      fout.close();
      return "ok";
  }
  </code></pre>
  
  <p>this is how I want to get images</p>
  
  <pre><code>&lt;img th:src="@{'images/' + ${post.imageName}}"/&gt;
  </code></pre>
  
  <p>for now I get 404 and when I want to view some images in directory I get </p>
  
  <pre><code>Fatal error reading PNG image file: Not a PNG file
  </code></pre>
  
  <p>how should I do it to make it work?</p>