servlet - 单击链接选择要下载的文件 [英] servlet - choosing the file to download on clicking a link
本文介绍了servlet - 单击链接选择要下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
场景如下:我有一个页面,当我们提交文件编号时,客户端会获得一个下载文件的链接。但我不知道如何编写一个servlet来决定将哪个文件发送给客户端。这必须在点击链接时发生,因此我无法发送可帮助我确定要下载哪个文件的参数。
The scenario is like : I have a page on which when we submit a file number, the client is given a link to download the file. But I don't know how to write one servlet which will decide which file to send to the client. This has to happen on clicking of a link, so I can't send parameters which can help me determine which file to download.
请帮忙。
推荐答案
- 你提交了一个文件号;
- 你根据该文件编号构建链接(它是GET的链接.GET可以有参数,因此您可以将参数放在URL路径或查询字符串中);
- 用户点击链接;
- 链接由servlet处理;
- servlet使用GET请求中的参数来确定要发送的文件;
- 您在回复中发送该文件的内容。
- you submit a file number;
- you build a link based on that file number (it's a link for a GET. GET can have parameters, so you can put parameters in the URL path or in the query string);
- user clicks the link;
- the link is handled by a servlet;
- the servlet uses the parameters from the GET request to determine what file to send;
- you send the content of that file in the response.
这些是步骤。
这是BalusC博客上的一个例子: http://balusc.blogspot.ro/2007/07/fileservlet.html
Here is an example on BalusC's blog: http://balusc.blogspot.ro/2007/07/fileservlet.html
您需要注意的是安全性:
What you need to take care of is security:
- 不要直接将文件公开为服务器上的路径,否则用户可以浏览路径以访问应用程序上的其他文件;
- 如果您的应用程序的用户拥有不同的权限,请确保在返回文件之前检查这些权限,以便用户无法访问其他人的文件。
这篇关于servlet - 单击链接选择要下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文