jsp中的下载选项窗口

发布于 2024-12-08 13:12:21 字数 137 浏览 1 评论 0原文

我需要在 jsp 中为用户提供一个选项来选择一个文件夹,他可以在其中保存/下载文件。请帮助我。

文本 input="file" 将提供文件选择器,但我需要 目录选择器

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就是爱搞怪 2024-12-15 13:12:21

HTTP 不允许您指定(服务器端)文件下载到的位置 - 这不是 jsp 特有的事情。

如果您需要这样做,那么您需要提供一个可嵌入的应用程序(javascript、java、flash、vbscript...),该应用程序允许在浏览器沙箱之外运行,并实现其自己的网络客户端来检索文件。这远非理想的解决方案。

您可以通过内容处置标头强制下载使用特定名称。

文本输入=“文件”将给出文件选择器

..但那是用于上传 - 不是下载。

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.

旧城空念 2024-12-15 13:12:21

无法使用 JSP/Servlet 在客户端计算机上设置下载文件的文件夹位置。如果您想添加文件夹选择器功能,那么您必须开发一个小程序。您可以使用JFileChooser来允许用户选择一个文件夹,并使用java.net.URLjava.net.URLConnection来下载文件。

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.

花心好男孩 2024-12-15 13:12:21

大多数浏览器会自动下载浏览器不渲染的文件,所以它只是一个链接......!例如,如果它是 zip 文件,只需将其添加为代码中的任何旧“链接”即可。当用户单击时,将启动下载/保存对话框......

“保存/下载”功能是客户端问题 - 请记住网络开发人员的工作是提供内容 - 浏览器决定如何处理内容。

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.

谜兔 2024-12-15 13:12:21

关键是 Content-Disposition 标头。它的值必须设置为attachment才能强制另存为对话框。您可以在 servlet 中完成这项工作。只需让您的链接 URL 指向文件 servlet,如下所示

<a href="fileservlet/filename.ext">download filename.ext</a>

然后,在文件 servlet 中(将上面的示例映射到 /fileservlet/* 的 URL 模式),执行以下操作:请

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.

参阅另外:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文