I need to give an option to user in jsp to choose a folder
where he can save/download a file. Please help me on the same.
the text input="file"
will give the file chooser but i need the directory chooser
I need to give an option to user in jsp to choose a folder
where he can save/download a file. Please help me on the same.
the text input="file"
will give the file chooser but i need the directory chooser
HTTP doesn't allow you to specify (server side) where a file is downloaded to - this is not a jsp specific thing.
If you need to this then you'd need to provide an embedable application (javascript, java, flash, vbscript...) which is allowed to operate outside the browser sandbox and implements its own network client for retrieving the file. Which is far from an ideal solution.
You can force the download to use a specific name via the content disposition header.
the text input="file" will give the file chooser
..but that's for uploads - not downloads.
You can't
set folder location at client machine of downloaded file using JSP/Servlet. If you want to add folder chooser feature then you have to develop an applet
. You may use JFileChooser
to allow user to select a folder and java.net.URL
and java.net.URLConnection
to download a file.
Most browsers will automatically download a file that the browser doesn't render, so it's just a link...! For example, if it's a zip file, just add it as any old "a link" in your code. When the user clicks , the download/save dialog will be launched ....
The "save/download" feature is a client issue -remember the web developers job is to provide content- it's the browser that decides how to deal with the content.
The key is the Content-Disposition
header. Its value has to be set to attachment
to force a Save As dialogue. You can do this job in a servlet. Just let your link URL point to a file servlet like so
<a href="fileservlet/filename.ext">download filename.ext</a>
Then, in the file servlet, which is for the above example to be mapped on an URL pattern of /fileservlet/*
, do the following:
String filename = URLDecoder.decode(request.getPathInfo().substring(1), "UTF-8");
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// Now get an InputStream of the file and write it to OutputStream of response.