How should we prompt user to select the path for downloading a file in java? Currently, I have provided a default path to store the files. But, how should I allow the user to select the path? I am using JSP and Servlet. Once user enters certain data in the JSP and clicks on Generate Report, the user should be prompted a dialog box asking him/her to choose the path.
Asked
Active
Viewed 2,230 times
0
-
Send a redirect to the generated report? – Markus W Mahlberg Apr 22 '15 at 20:33
1 Answers
0
The path where the file will be downloaded is job of the client browser, not from the web application. You should write the content of the file to download into the HttpServletResponse
. Make sure to set the content type to "application/octet-stream"
in order to always prompt the user for downloading the file, regardless of the client browser.

Luiggi Mendoza
- 85,076
- 16
- 154
- 332
-
I think `Content-Disposition: attachment;` header is a better choice. Have a look at this [answer](http://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download#20509354) for better understanding – Arkantos Apr 22 '15 at 17:28
-
@Arkantos I've read that post in the past, but still prefer to use content type rather than content disposition. – Luiggi Mendoza Apr 22 '15 at 17:29
-
Any specific reason for this preference ? Just want to know if there are any pitfalls with the approach in that link.. It looks clean to me :-) – Arkantos Apr 22 '15 at 17:34
-
@Arkantos sometimes I don't know what kind of file I'm downloading, it can be anything. So, instead of letting the browser guess the type or anything, I prefer to set application/octet-stream as a way to say *don't try anything, just download it*. In specific cases when you have control of the type of files being downloaded, I recommend using content-disposition next to content-type. – Luiggi Mendoza Apr 22 '15 at 17:39
-
Exactly if we know what we're downloading then `Content-type + Content-disposition` headers are good otherwise we can fallback to `application/octet-stream`.. Thanks for clarifying that again :-) – Arkantos Apr 22 '15 at 17:57