6

I want my program to have a pop-up save as window option before file start downloading, however when I run my servlet it automatically starts downloading the file. What am I missing here ?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletOutputStream outputStream = response.getOutputStream();
    FileInputStream fis=new FileInputStream("E:/sound.mp3");
    response.setContentLength(fis.available());
    response.setContentType("audio/basic");
    response.addHeader("content-disposition", "attachment;filename=abc.mp3");
    while(true){
        int read = fis.read();
        if(read==-1)break;
        outputStream.write(read);
    }
    fis.close();        
}
Santhosh
  • 8,181
  • 4
  • 29
  • 56
Hello World
  • 944
  • 2
  • 15
  • 39
  • I believe that this is on the server side. You're trying to propagate a server event to the client side. – christopher Nov 03 '14 at 17:39
  • Take a look at: http://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click-for-pdf-in-html and http://stackoverflow.com/questions/2226192/generate-some-xml-in-javascript-prompt-user-to-save-it Regards – Danilo Muñoz Nov 03 '14 at 18:11
  • 1
    @DaniloMuñoz:AS fas as I can see avobe two scenarios are different form mine..but thanks for concern – Hello World Nov 03 '14 at 18:14
  • What about your client? Browser, standalone/desktop app, ... – Danilo Muñoz Nov 04 '14 at 11:18
  • @DaniloMuñoz:Yes it's standalone/desktop. I was doing this just for practise. I don't find anything wrong with my code though. – Hello World Nov 04 '14 at 13:09
  • how do you call your servlet? – Ken de Guzman Nov 11 '14 at 05:36
  • 1
    @HelloWorld Provide additional info like how do you access your `servlet` and where do you run the servlet – Santhosh Nov 11 '14 at 06:26
  • Possible duplicate of http://stackoverflow.com/questions/15483637/how-to-use-a-save-file-dialog-from-a-servlet – Am_I_Helpful Nov 11 '14 at 06:30
  • @user23123412: I am not calling my servlet, just running it in eclipse. – Hello World Nov 11 '14 at 16:57
  • @SanKrish: I am not accessing my servlet anywhere form out side. – Hello World Nov 11 '14 at 16:58
  • 1
    @shekharsuman that's not a duplicate option. – Luiggi Mendoza Nov 11 '14 at 21:18
  • This seems like a web application, not a standalone/desktop app. If this is your case, then just set the content type as `"application/octet-stream"` which is the generic content type for file download (regardless its type). – Luiggi Mendoza Nov 11 '14 at 21:20
  • @LuiggiMendoza: Making the content type "application/octet-stream" also didn't fix my problem....I tried. – Hello World Nov 12 '14 at 19:30
  • Then I don't understand what's your exact problem. Please provide more info about what you're doing, how you're executing the code and how your application is consuming it. – Luiggi Mendoza Nov 12 '14 at 19:31
  • Well ! This is my all code, except I didn't provide the class name because that wasn't necessary(I assume), and I am running by code in eclipse simply by right clicking and _run as--> run on server_. What is happening here the mp3 gets downloading automatically which I don't want. I need to pop up a **Save as** box first that's it. – Hello World Nov 12 '14 at 19:37
  • Your program is NOT desktop/standalone, since it is a servlet running on a server. The client is saving the information you sent, NOT your program. See my complete answer below. – Marcelo Glasberg Nov 13 '14 at 23:30
  • "What am I missing here" dome code to pop up the window. What makes you think this should happen automatically? – Raedwald Nov 14 '14 at 13:13

4 Answers4

6

Your program is NOT desktop/standalone, since it is a servlet running on a server. When you run it in Eclipse by right clicking and run as -> run on server, Eclipse actually opens a web page to display the results. Therefore, your program is now a Web application, and Eclipse (or the page it opens) is the client. The client is saving the information you sent, NOT your program. Got it?

The content-disposition header is there only to suggest the file name of the download. The browser settings define if it will open a Save As Window or not. You cannot control that.

For example, in Google Chrome, in Setting/Advanced Setting/Downloads, there is the option Ask where to save each file before downloading. Only if this option is selected it will open the dialog you want. Otherwise it will save it in a default location (also defined in the browser settings). Similar options exist for all browsers.

Please also note that, depending on the content-type header, the browser will try to display the content, and not download it. For example, the browser will try to display texts and html. But then you can force the download by setting the header to a non-displayable type:

response.setContentType("application/octet-stream");

In case you don't want to create a Web app: Since your program runs on a server, it simply sends the information and is done. It is the client program who decides what to do with it. In your present case the client is a browser (or Eclipse opening a browser page). Headers such as the content-disposition header are aimed at browsers. If you are to create your own client (Swing client, Android app, iPhone app) which is NOT a browser, then the client will receive the information from the server and decide what to do with it (display it, or save it in any way), even ignoring the HTTP headers.

Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
2

Try looking here: http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm

take out the main statement in their code and put run(new FileChooserTest(), 250, 110); in your own code. If I were doing it, I would make an int named saveStatus and 3 finals that equal 0, 1, and 2 named waiting, save, and cancel. Then I would do a while loop in your other programming to see if saveStatus was equal to waiting to pause your program (but not the dialog). Afterwards, I would make an if statement to see if saveStatus was equal to save. If so, download it, and if not, don't. Simple as that.

mirvine
  • 126
  • 1
  • 2
  • 14
2

Your problem is the Mime-Type. Some types (especially those where a specific handler is known) will be downloaded directly by most browsers. It does help a bit to use application/binary, but even then some browsers might be configured to download it or interpret the file name extension in the disposition handler.

I think most solutions use javascript on the page before the download link.

eckes
  • 10,103
  • 1
  • 59
  • 71
-1

You have to implement the dialog manually, e.g. (http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html). After user selecting the file, you will be able to start the http request download (to your servlet) and save the file to the desired path.

Danilo Muñoz
  • 623
  • 1
  • 7
  • 17