Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();        
}
like image 329
Hello World Avatar asked Nov 03 '14 17:11

Hello World


People also ask

What is content disposition PDF?

The Content-Disposition response header tells the browser to download a file rather than displaying it in the browser…

What is content disposition used for?

In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

What is content disposition filename?

Content-Disposition is an optional header and allows the sender to indicate a default archival disposition; a filename. The optional "filename" parameter provides for this. This header field definition is based almost verbatim on Experimental RFC 1806 by R. Troost and S.

What is content disposition in Python?

September 28, 2022 March 15, 2022 by Holistic SEO. The Content-Disposition HTTP Header response header is a header that indicates whether the content will be displayed inline in the browser.


2 Answers

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.

like image 162
MarcG Avatar answered Sep 28 '22 01:09

MarcG


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.

like image 33
mirvine Avatar answered Sep 28 '22 02:09

mirvine