Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:58:56+00:00 2026-06-15T07:58:56+00:00

I would like to stream my audio/video files to web using servlet. I made

  • 0

I would like to stream my audio/video files to web using servlet.

I made an attempt with the following servlet:

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());
}

And the following HTML:

<a href="/media" data-format="mp3 ogg">Click Here!</a>

However, the player is just loading… loading… loading…

How is this caused and how can I solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:58:57+00:00Added an answer on June 15, 2026 at 7:58 am

    A lot of media players require that the server supports the so-called HTTP range requests. I.e. it must be able to return specific parts of the media file on request with a Range header. For example, only the bytes at exactly the index 1000 until with 2000 on a file of 10MB long. This is mandatory for many media players in order to be able to skip a certain range of the media stream quickly enough and/or to improve buffering speed by creating multiple connections which each requests different parts of the file.

    This is however a lot of additional code in your servlet which requires a well understanding of the HTTP Range specification. Usually the servletcontainer’s (Tomcat, JBoss AS, Glassfish, etc) own default servlet already supports this out the box. So if there’s a way to publish the media folder into the web by standard means, so that you don’t need to homegrow a servlet for this, then I’d go on this route.

    It’s unclear which servletcontainer you’re using, so I’ll assume Tomcat in this example:

    1. Just drop love.mp3 file in the public web content of the web project, so that it’s just available by <a href="love.mp3"> without the need for a whole servlet.

    2. Or, put the love.mp3 file in a new subfolder of Tomcat/webapps folder, e.g. Tomcat/webapps/media/love.mp3. This way it’s available by <a href="/media/love.mp3">.

    3. Or, put the love.mp3 file elsewhere on disk, e.g. /path/to/media/love.mp3 and add the /media folder as new context by adding the following line to Tomcat’s /conf/server.xml:

       <Context docBase="/path/to/media" path="/media" />
      

      This way it’s available by <a href="/media/love.mp3"> as well.

    Either way, Tomcat’s own DefaultServlet, which has proper support for Range requests, will be used to stream the content.

    But if there’s absolutely no way to make use of servletcontainer’s own default servlet, then you need to rewrite your servlet code in such way that it properly supports Range requests. You can get inspiration from open source examples such as Tomcat DefaultServlet and OmniFaces FileServlet.

    See also:

    • Video Using HTML 5 and servlet
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to segment a part of an image (video stream), between 2
I am using VideoView to play an mp4 video. I would like to give
I would like to stream some files in and out of cassandra since we
I would like to have nice player in the iPhone. The source is audio/video
I would like to get an audio stream from URL and play it on
Using C# and NAudio, I have have three wave files I would like to
I have to create a client/server system to stream video and audio. It would
I'm using Node.js, Express (and connect), and fluent-ffmpeg. We want to stream audio files
We have a whole catalogue of videos which we would like to stream to
I would like to redirect the Debug stdout stream to a textblock. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.