downloading file with save as dialog box in jsp…ho

2019-05-26 07:27发布

问题:

I am having one jsp in which i have provided one link for downloading any document.

Whenever user clicks the link,it directly open the document.

I want to show one dialog box(which generally appears on many websites) which will ask user to save, cancel and view the document.

can anybody help me out..??

i am using following code in my controller class.

InputStream is = new FileInputStream(new File(**File Path**);

              IOUtils.copy(is, response.getOutputStream());
              response.flushBuffer();

回答1:

I got the solution .

I added response content type and it's working.

response.setHeader("Content-Disposition", "attachment;filename=abc.jpg");

Thanks to all who tried to help me out from this issue.



回答2:

The action the browser takes depends on a couple of things; the MIME type that the server sends it for the file, but also on how the browser is configured to handle that particular MIME type. You can set the MIME type on the server end with:

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

That MIME type should generally show a save dialog, but the user could potentially configure their browser to handle it differently.



回答3:

As Nick Wilson said, you will also have to check the browser how octet-stream is handled...