title: How can access local file from localhost
body: <p>I'm developing a web application with spring boot. In application, I upload a file and copy to the local folder after that file absolute path is saved in the database. So I have file and I know where it is but I can't reach it in localhost. I know in spring has a static folder for static files but I don't want to copy a file because I will use this data in other applications.</p>
  
  <p>For example:</p>
  
  <p>Local file location: <code>/Users/user/data/image.png</code></p>
  
  <p>I want to reach like that: <code>http://localhost:8080/data/image.png</code></p>
  
  <hr>
  
  <p><strong>Edit: I find the solution.</strong> I used <code>MvcUriComponentsBuilder</code> for this.</p>
  
  <pre><code>String url = MvcUriComponentsBuilder.fromMethodName(FileController.class,"serveFile",resource.getFilename()).build().toString();
  </code></pre>
  
  <p>it's return right value of controllers method.</p>
  
  <p>Method:</p>
  
  <pre><code>@GetMapping("/files/{filename:.+}")
      @ResponseBody
      public ResponseEntity&lt;Resource&gt; serveFile(@PathVariable String filename) {
  
          Resource file = storageService.loadAsResource(filename);
          return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                  "attachment; filename=\"" + file.getFilename() + "\"").body(file);
      }
  </code></pre>