?
Solved

MP3 download using JSP/Servlet

Posted on 2005-04-12
9
Medium Priority
?
1,563 Views
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 java.io.*;
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) {
            response.setContentType("application/binary");
            out.println("<H1>Parameter missing</H1>");
        } else {
            response.setContentType("audio/mpeg");
             response.setHeader("Content-Disposition","filename="+filename+";");

                BufferedInputStream fif = new BufferedInputStream(new FileInputStream(filename));
              int data;
             byte[] buffer = new byte[4096];
             while((data = fif.read(buffer))!=-1) {
                  out.write(buffer);
             }
             fif.close();
             out.close();
        }
    }
}


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.
0
Comment
Question by:dhaval_8011
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 13769145
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.

FWIW
Rod
0
 
LVL 1

Expert Comment

by:hgalindo
ID: 13776154
You are setting  the header in a wrong way.

response.setHeader("Content-Disposition","filename="+filename+";");

You should use the header like this:

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

Author Comment

by:dhaval_8011
ID: 13776304
Ok.
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);

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:hgalindo
ID: 13776412
Use this header combination

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

where nombreArchivoParaUsuario is like "archivo.mp3"
0
 

Author Comment

by:dhaval_8011
ID: 13776623
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.
0
 
LVL 1

Accepted Solution

by:
hgalindo earned 2000 total points
ID: 13776695
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
0
 

Author Comment

by:dhaval_8011
ID: 13784275
Thank you. It works now. Yeahh I am done.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This video will demonstrate how to customize windows, tools, and control bars, and save them as screen sets. Open and resize windows: Customize the toolbar: Customize the control bar: Customize your tool selections: Your screen set is alread…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 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