MP3 download using JSP/Servlet

Posted on 2005-04-12
Last Modified: 2012-08-13
Hello Experts,

Here is what my problem is - I  am making a music upload download site where you can go and download music. I implemented the upload music part and it works fine. But I cant get the download part to work. Basically the user would just click a link and it should start downloading the .mp3 file. The file are in a directory on the webserver machine. Here is what I have done so far

package IPRO330;

 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.rmi.RemoteException;

public class Download extends HttpServlet
    /** This method processes the HTTP Get request
      * @param   HttpServletRequest  request  the request object.
      * @param   HttpServletResponse response  the response object.
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        String filename = "C:\\Kansas.mp3";
            ServletOutputStream out = response.getOutputStream();
        if (filename == null) {
            out.println("<H1>Parameter missing</H1>");
        } else {

                BufferedInputStream fif = new BufferedInputStream(new FileInputStream(filename));
              int data;
             byte[] buffer = new byte[4096];
             while((data =!=-1) {

I am using IE and what this does is instead of poping the  save as or open dialog it prints the file on the html page. Could you please tell me what I am doing wrong or if there is a better way to this.

Thank you in advance.
Question by:dhaval_8011
    LVL 29

    Expert Comment

    You probably will need to check your web servers mime type registration.

    However, your results with IE are not what I would expect given the code above.  In fact, I would say your mime type mappings are messed up on your client machine, but I don't have enough information to tell at this point.
    I'd at least try testing against FireFox first.

    Try "application/binary" or "application/mp3" for the download part as well.

    If you are still having difficulty, you definately should look at the webserver.

    LVL 1

    Expert Comment

    You are setting  the header in a wrong way.


    You should use the header like this:

    response.addHeader("Content-disposition","attachment; filename="+ filename);

    Author Comment

    I tried the change you mentioned and now it shows the save as dialog box. But the file name and file type are not displayed correctly.
    The file type is blank and the file name is download which is the name of my servlet. Any idea why that might be. If it helps all I changed in my code is this line

    response.addHeader("Content-disposition","attachment; filename="+ filename);

    LVL 1

    Expert Comment

    Use this header combination

    response.setContentType ("binary/octec-stream");
    response.addHeader ("Content-disposition", "attachment; filename=" + nombreArchivoParaUsuario);

    where nombreArchivoParaUsuario is like "archivo.mp3"

    Author Comment

    If is set the content type as binary/octec-stream it prints contents of the file onthe html page. Again I am using IE 6.0. The program works fine on FireFox and Netscape.
    LVL 1

    Accepted Solution

    change your code like this

    response.setContentType ("audio/mpeg");
    response.addHeader ("Content-disposition", "attachment; filename=" + filename);

    This time, filename MUST NOT have c:\\ or paths... use only the name "Kansas.mp3" not "C:\\Kansas.mp3"

    use 2 separate variables for your inputstream and the name for download

    Author Comment

    Thank you. It works now. Yeahh I am done.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Viewers will learn how to use Macros for greater control over Rack parameters in Ableton Live. Group devices into a Rack by selecting them and pressing Command-G (Ctrl-G on PC): Control-click (Right Click on PC) a parameter to access pop-up menu, …
    Viewers will learn how to include realistic velocity sensitivity to their Sampler instruments. Set the Vol<Vel parameter in the Filter/Global tab to your desired setting: Gather samples of hits of various intensity, and drag/drop into Velocity zon…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now