1

I develop music site.

On jsf page I have HTML audio tag
If audio file located in parent of WEB-INF for example

WebContent |-- META-INF |-- WEB-INF
-- some.mp3
I just write next piece of code:

<audio ...>
  <source src="#{request.contextPath}/some.mp3" type="audio/mpeg" />
</audio>

And after adding controls I could play/pause this audio.

But what should I do if my audio file does not store locally in WebContent folder?
I know that audio files should store in DB like BLOB.
After downloading selected audio from DB how should I represent it?(String array, some kind of Stream?)

In all examples that I saw in the Web peoples play local audio files.
So, how to play audio (on jsf page) that store in database???

I hope you understand that I want.

Bohdan Zv
  • 442
  • 2
  • 5
  • 22
  • 3
    This has been answered several times in different flavors, but all boils down to creating a servlet like this http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page or http://stackoverflow.com/questions/1812244/simplest-way-to-serve-static-data-from-outside-the-application-server-in-a-java Which one do you accept as dupe? – BalusC Apr 28 '15 at 16:43
  • @BalusC I think first one will be better in my case.. But I have several questions about how it works.. First of all, I will have static folder called `audio` in parent of `WEB-INF` and `source` tag will looks like ``.... Secondly, code above will ask AudioServlet to download audio from database, translate it into `Stream` and set it like `OutputStream` in `response`??? – Bohdan Zv Apr 28 '15 at 17:09

1 Answers1

1

You can not read a file directly from the database : you have to serve it to the client.

It works with files inside the WebContent folder because those files are served automatically by the web server.

To serve file stored inside the database you'll need to write a FileServlet. Basically this file servlet will:

  • respond to a specific request;
  • retrieve the file from the database;
  • output the file content to the user with a stream.

Have a look at this for a good start on FileServlet.

ForguesR
  • 3,558
  • 1
  • 17
  • 39