login about faq

I try to write up some code to return the audio file (mp3) stream back to client site via JSP. How I can do that?

asked Jul 05 '11 at 16:11

guru4us's gravatar image

guru4us
111231

edited Feb 27 '12 at 13:36


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's gravatar image

guru4us
111231

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×3
×1
×1
×1
×1

Asked: Jul 05 '11 at 16:11

Seen: 1,044 times

Last updated: Feb 27 '12 at 13:36

powered by OSQA