dhaval_8011
asked on
MP3 download using JSP/Servlet
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("a pplication /binary");
out.println("<H1>Parameter missing</H1>");
} else {
response.setContentType("a udio/mpeg" );
response.setHeader("Conten t-Disposit ion","file name="+fil ename+";") ;
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.
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("a
out.println("<H1>Parameter
} else {
response.setContentType("a
response.setHeader("Conten
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.
You are setting the header in a wrong way.
response.setHeader("Conten t-Disposit ion","file name="+fil ename+";") ;
You should use the header like this:
response.addHeader("Conten t-disposit ion","atta chment; filename="+ filename);
response.setHeader("Conten
You should use the header like this:
response.addHeader("Conten
ASKER
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("Conten t-disposit ion","atta chment; filename="+ filename);
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("Conten
Use this header combination
response.setContentType ("binary/octec-stream");
response.addHeader ("Content-disposition", "attachment; filename=" + nombreArchivoParaUsuario);
where nombreArchivoParaUsuario is like "archivo.mp3"
response.setContentType ("binary/octec-stream");
response.addHeader ("Content-disposition", "attachment; filename=" + nombreArchivoParaUsuario);
where nombreArchivoParaUsuario is like "archivo.mp3"
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. It works now. Yeahh I am done.
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