?
Solved

downloading file to user machine

Posted on 2007-03-22
12
Medium Priority
?
197 Views
Last Modified: 2010-04-01
Hi experts,

I have mp3 files in the server and I have jsp page that display all these files. I need when the user click on the "save", the file is downloaded to his machine.

Thanks
0
Comment
Question by:Sh_Rashed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18777930
you can just create a link to the file and browser will download it...

<a href="yourfile">Filename</a>
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 260 total points
ID: 18785746
If you don't want to disclose the exact URL of the MP3 (or if it is kept somewhere other than the context root of the web app), you can make it downloadable through a Servlet:

http://jspwiki.org/wiki/MakingADownloadServlet
0
 

Author Comment

by:Sh_Rashed
ID: 18787620
I have tried the code in the url

 System.out.println("My file is:111111111111111111"+request.getParameter("file"));
   
    String  filename = request.getParameter("file");
    String original_file = "/CourseFiles"+filename;
   
        File                f        = new File(filename);
        int                 length   = 0;
        ServletOutputStream op       = response.getOutputStream();
        ServletContext      context  = getServletConfig().getServletContext();
        String              mimetype = context.getMimeType( filename );
   
       response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
       response.setContentLength( (int)f.length() );
       response.setHeader( "Content-Disposition", "attachement; filename=\"" + original_file+ "\"" );
        byte[] bbuf = new byte[1000];
        DataInputStream in = new DataInputStream(new FileInputStream(f));

        while ((in != null) && ((length = in.read(bbuf)) != -1))
        {
            op.write(bbuf,0,length);
        }

        in.close();
        op.flush();
        op.close();
       

but I got this Exception  1.mp3 (The system cannot find the file specified)

the file I want to download it is mps file audio
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 240 total points
ID: 18787659
you will need the mp3's absolute file path in your servlet..before you can get it..

in the init method of the servlet you can get hold of your servletContext

servletCOntext sc = getServletConfig().getServletContext();

in your doGet /doPost you will need following.

String  filename = request.getParameter("file");
    String original_file = "/CourseFiles"+filename;
    String abspath = sc.getRealPath(filename);

        File                f        = new File(abspath);
0
 

Author Comment

by:Sh_Rashed
ID: 18787687
Thank u so much
0
 

Author Comment

by:Sh_Rashed
ID: 18787735
it works fine in firefox, but not in IE7?

help plz? :)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18787841
what error you are getting??
0
 

Author Comment

by:Sh_Rashed
ID: 18787922
the quicktime plugin in the IE7 shows question mark image
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18788013
instead of opening up directly to quicktime, save to the disc and then run it with quicktime to see if the file is coming down correctly
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18788761
Then the quicktime plugin is not properly configured with IE.
0
 

Author Comment

by:Sh_Rashed
ID: 18788801
but when I open the url of mp3 in IE, quicktime plays the file.

anyway, Thanks for helping me :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18788831
>> response.setHeader( "Content-Disposition", "attachement; filename=\"" + original_file+ "\"" );

attach'E'ment with an extra E in between or attachment? I guess that is the issue. One of the versions of IE had this bug, not sure which one.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

741 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