downloading file with save as dialog box in jsp…ho

2019-05-26 07:37发布

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

3条回答
萌系小妹纸
2楼-- · 2019-05-26 08:00

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

查看更多
Lonely孤独者°
3楼-- · 2019-05-26 08:08

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.

查看更多
劫难
4楼-- · 2019-05-26 08:23

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.

查看更多
登录 后发表回答