Hello World
Hello World

Reputation: 983

Having issue while downloading a file using content-disposition

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();        
}

Upvotes: 7

Views: 6358

Answers (4)

eckes
eckes

Reputation: 10423

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.

Upvotes: 2

Marcelo Glasberg
Marcelo Glasberg

Reputation: 30879

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.

Upvotes: 7

mirvine
mirvine

Reputation: 126

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.

Upvotes: 2

Danilo Muñoz
Danilo Muñoz

Reputation: 623

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.

Upvotes: -1

Related Questions