servlet - 单击链接选择要下载的文件 [英] servlet - choosing the file to download on clicking a link

查看:216
本文介绍了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.

请帮忙。

推荐答案


  1. 你提交了一个文件号;

  2. 你根据该文件编号构建链接(它是GET的链接.GET可以有参数,因此您可以将参数放在URL路径或查询字符串中);

  3. 用户点击链接;

  4. 链接由servlet处理;

  5. servlet使用GET请求中的参数来确定要发送的文件;

  6. 您在回复中发送该文件的内容。

  1. you submit a file number;
  2. 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);
  3. user clicks the link;
  4. the link is handled by a servlet;
  5. the servlet uses the parameters from the GET request to determine what file to send;
  6. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆