title: How to stream audio/video files such as MP3, MP4, AVI, etc using a Servlet
body: <p>I would like to stream my audio/video files to web using servlet.</p>
  
  <p>I made an attempt with the following servlet:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      File file = new File("/Users/myfolder/Documents/workspace/love.mp3");
      response.setContentType(getServletContext().getMimeType(file.getName()));
      response.setContentLength((int) file.length());
      Files.copy(file.toPath(), response.getOutputStream());
  }
  </code></pre>
  
  <p>And the following HTML:</p>
  
  <pre><code>&lt;a href="/media" data-format="mp3 ogg"&gt;Click Here!&lt;/a&gt;
  </code></pre>
  
  <p>However, the player is just loading... loading... loading...</p>
  
  <p>How is this caused and how can I solve it?</p>