• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

downloading file to user machine

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
Sh_Rashed
Asked:
Sh_Rashed
  • 5
  • 4
  • 3
2 Solutions
 
KuldeepchaturvediCommented:
you can just create a link to the file and browser will download it...

<a href="yourfile">Filename</a>
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Sh_RashedAuthor Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
KuldeepchaturvediCommented:
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
 
Sh_RashedAuthor Commented:
Thank u so much
0
 
Sh_RashedAuthor Commented:
it works fine in firefox, but not in IE7?

help plz? :)
0
 
KuldeepchaturvediCommented:
what error you are getting??
0
 
Sh_RashedAuthor Commented:
the quicktime plugin in the IE7 shows question mark image
0
 
KuldeepchaturvediCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Then the quicktime plugin is not properly configured with IE.
0
 
Sh_RashedAuthor Commented:
but when I open the url of mp3 in IE, quicktime plays the file.

anyway, Thanks for helping me :)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now