Web tier: servlets, JSP, Web frameworks: MP3 Response

  1. MP3 Response (2 messages)

    I'm attempting to write a servlet to serve audio to a website. My over all goal is to provide a 30-45 second preview of an MP3 embedded with SMIL or QT (but right now I am just trying to play anything). I'd like the result to also play on the iPhone (this QT) but be relatively seem less across different platforms. I'm having trouble with the servlet first and foremost at this point. I've been following the example in O'Reilly's JSP & Servlet Cookbook (code attached) but cannot get the audio to play when I access the servlet. I've changed the content type to text/html to see if I'm even reading the file and I get back the character's of the MP3 (looks the same as if I opened the MP3 in a text editor) so it seems I am reading and streaming the file correctly. But no matter what format I use, I cannot embed the stream to a website. I have also tried playing the URL in the QT player to no avail. import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SendMp3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileName = (String) request.getParameter("file"); if (fileName == null || fileName.equals("")) throw new ServletException( "Invalid or non-existent file parameter in SendMp3 servlet."); if (fileName.indexOf(".mp3") == -1) fileName = fileName + ".mp3"; String mp3Dir = getServletContext().getInitParameter("mp3-dir"); if (mp3Dir == null || mp3Dir.equals("")) throw new ServletException( "Invalid or non-existent mp3Dir context-param."); ServletOutputStream stream = null; BufferedInputStream buf = null; try { stream = response.getOutputStream(); File mp3 = new File(mp3Dir + "/" + fileName); //set response headers response.setContentType("audio/mpeg"); response.addHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentLength((int) mp3.length()); FileInputStream input = new FileInputStream(mp3); buf = new BufferedInputStream(input); int readBytes = 0; //read from the file; write to the ServletOutputStream while ((readBytes = buf.read()) != -1) stream.write(readBytes); } catch (IOException ioe) { throw new ServletException(ioe.getMessage()); } finally { if (stream != null) stream.close(); if (buf != null) buf.close(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } Any insights would be MUCH appreciated! Thank you, Joe

    Threaded Messages (2)

  2. read problem?[ Go to top ]

    It may be that you're not reading the whole MP3 file. Your (O'Reilly's) code is reading the bytes from the MP3 file one at a time, and stopping when it gets a byte that equates to -1. However, in binary files, it is entirely possible that you would encounter a byte that "equals" -1 and yet not be at the end of the file. Instead, read bytes into a buffer until you can't read any more: buf = new BufferedInputStream(input); int readBytes = 0; byte[] buffer = new byte[8192]; //read from the file; write to the ServletOutputStream while ((readBytes = buf.read(buffer)) > 0) stream.write(buffer, 0, readBytes); That will read bytes from the MP3 file in 8k chunks, stopping when there is nothing left to read. hth, -Peter
  3. Read problem[ Go to top ]

    I don't know solution. But I think that code for read mp3 file is correct. Condition : while ((readBytes = buf.read(buffer))!= -1) is correct. http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[])