Couple of steps to make this happen
-
First need to tell JSP you try to output other content rather than a regular html page, so you need to put the code like this at the beginning.
<%@ page contentType="audio/mpeg" %>
-
Second need to get the audio file, my below example is getting from a URL link
URL url = new URL(wsurl+filename);
URLConnection c = url.openConnection();
//set timeout
//c.setConnectTimeout(2000);
InputStream is = c.getInputStream();
BufferedInputStream in = new BufferedInputStream(is);
ServletOutputStream sos = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(sos);
-
and then, need to write out to the servlet buffer. do something like this
while ((i = in.read()) != -1)
{
bos.write(i);
}
bos.flush();
bos.close();
-
finally you might need some code to handle exception. and redirect the page to an error page
catch (Exception e) {
out.println("An exception occured: " + e.getMessage());
session.setAttribute("error_msg", e.getMessage()) ;
response.sendRedirect("./Error");
}
It seems work for me fine via this way..
answered
Jul 05 '11 at 16:24
guru4us
1●6●23