如何创建“别名” 在 Apache Tomcat 中?
我正在开发一个允许用户上传附件的网络应用程序。 这些附件存储在与 Web 应用程序不同的驱动器上。 如何为此驱动器创建别名(相当于 Apache HTTP 服务器的别名)以便用户可以下载这些附件?
目前我正在创建一个上下文文件并将其转储到 CATALINA_HOME/conf/Catalina/localhost 中,但它经常被随机删除。 上下文文件名为attachments.xml,内容如下所示。 我也读过有关虚拟主机的内容,但如果我理解正确,那么虚拟主机不是我要找的。 我使用的是 Apache Tomcat 6.0.18 版本。
attachments.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase = "e:\uploads\attachments"
reloadable = "true"
crossContext = "true">
</Context>
I am working on a web application that allows users to upload attachments. These attachments are stored on a different drive than that of the web application. How can I create an alias (equivalent to Apache HTTP server's aliases) to this drive so that users can download these attachments?
Currently I am creating a context file and dumping it in CATALINA_HOME/conf/Catalina/localhost, but it gets randomly deleted every so often. The context file is named attachments.xml and the contents are shown below. I have also read about virtual hosts, but if I understand correctly, then a virtual host is not what I am looking for. I am using version 6.0.18 of Apache Tomcat.
attachments.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase = "e:\uploads\attachments"
reloadable = "true"
crossContext = "true">
</Context>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我花了很多时间研究这个问题,找到了解决上下文文件随机删除的解决方案。 我在 Apache 网站的主机配置部分找到了这段摘录:
虚拟主机存储在位于 CATALINA_HOME\conf 的 server.xml 文件中。 Tomcat 配置为 localhost 作为默认主机。 因此,如果我们添加第一篇文章中 attachments.xml 的内容,我们会得到以下内容:
我认为,这与定义类似于 Apache 的 HTTP 服务器的别名非常接近。
I spent a lot more time researching this and found a solution that solves the random deletion of the context files. I found this excerpt on Apache's website under the host configuration section:
The virtual hosts are stored in the server.xml file located at CATALINA_HOME\conf. Tomcat comes configured with localhost as the default host. So, if we add the contents of attachments.xml from the first post, we get the following:
This is as close as one can get to defining aliases similar to Apache's HTTP server, I think.
有多种选择。
各有一些缺点和一些优点。 出于多种原因,我强烈喜欢第一个解决方案:
关于下载 servlet:
这样,您将有一个 servlet 为您的静态资源提供服务,您可以将其绑定到 url“/download/*”(例如,在还处理文件上传的应用程序中)您将获得:
第三个选项有一些严重的缺点,如果您不特别注意它们,就会容易受到攻击:
<% System.exit(0); 上传“index.jsp” %>
或更多恶意内容。另一种想法:您不需要额外的
crosscontext="true"
。 这意味着您部署的只是为了提供文件服务的 Web 应用程序可以访问其他 Web 应用程序,例如能够管理它们或访问它们的私有数据。 通常你根本不需要那个,就你的问题而言,你肯定不想要那个。There are multiple options.
each has some drawbacks and some advantages. I strongly prefer the first solution for multiple reasons:
About the download servlet:
This way you'd have a servlet serving your static resources, which you might bind to the urls "/download/*" (e.g. in the application that also handles file uploads) You'd gain:
The third option has some severe drawbacks and opens you for attacks if you don't take special care of them:
<% System.exit(0); %>
or more malicious content.One additional thought: You don't need the extra
crosscontext="true"
. This would imply that the webapplication that you deploy just to serve your files has access to other webapplications, e.g. is able to manage them or access their private data. Usually you don't need that at all, in the case of your question you definitely don't want that.请参阅我的新问题的初始部分,了解通过编辑 context.xml 文件来执行此操作的方法
如何向 Servlet 添加别名java 中的上下文?。
据一些人称,出于性能原因,不再需要(2012 年:Tomcat 6 或 7)使用 Apache 而不是 Tomcat 来提供静态内容。
See the initial part of my newer question for ways to do this by editing the context.xml file
How do I add aliases to a Servlet Context in java?.
According to several people now, it is no longer necessary (2012: Tomcat 6 or 7) to use Apache for performance reasons over Tomcat for serving static content.