0

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

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Chandu
  • 75
  • 1
  • 3
  • 11
  • I think it depends on the browser whether it shows where to save or not. Google chrome just downloads it unless you right click and choose save as. Not sure though – Tjekkles Oct 07 '11 at 11:38

4 Answers4

1

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.

symcbean
  • 47,736
  • 6
  • 59
  • 94
1

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.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

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.

jayunit100
  • 17,388
  • 22
  • 92
  • 167
0

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.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555