0
votes

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.

1
This has been answered several times in different flavors, but all boils down to creating a servlet like this stackoverflow.com/questions/2340406/… or stackoverflow.com/questions/1812244/… Which one do you accept as dupe?BalusC
@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 <source src="#{request.contextPath}/audio/#{myBean.audioTitle}" type="audio/mpeg" />.... Secondly, code above will ask AudioServlet to download audio from database, translate it into Stream and set it like OutputStream in response???Bohdan Z.

1 Answers

1
votes

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.