从 servlet 容器中与上下文目录不同的文件夹提供文件

发布于 2024-12-23 08:21:52 字数 272 浏览 2 评论 0原文

我遇到了一种情况,我必须提供来自不同文件夹的文件,然后是我的网络应用程序正在运行的上下文之一。例如,假设我的 Web 应用程序在“/opt/tomcat/webapps/ROOT/”上的 servlet 上下文中运行,并且我必须提供“/opt/my_other_folder/”中存在的文件。这些文件夹可以由客户端在运行时更改,因此我不能简单地添加指向这些目录的新上下文。我想要一个解决方案,我不必为此重写网络服务器。另外,我所从事的产品是通用的,因此我无法提供特定于某些 servlet 容器的解决方案。

谢谢!

I got a situation that I must serve files from different folders then the one of the context my web app is running. As an example, suppose my web app is running in a servlet context on "/opt/tomcat/webapps/ROOT/" and I must serve files existent in "/opt/my_other_folder/". These folders can be changed in runtime by the client, so I can't simply add a new context pointing to these directories. I would like a solution that I wouldn't have to rewrite a web server only for that. Also, the product I work on is generic, so I can't have a solution specific to some servlet container.

Thanks!

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

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

发布评论

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

评论(1

最初的梦 2024-12-30 08:21:52

如果您只提供文件服务,我会考虑在您的 servlet 容器前面使用 Apache HTTP Server 之类的东西,您可以在其中简单地使用其各种指令来提供指向易于配置的位置的“虚拟目录”。

否则,您可以编写和配置一个标准 Java servlet,它基本上会执行相同的操作 - 将实际路径存储在由 servlet 读取的 Java 属性文件中。虽然这并不是很多工作,但它比上述 Apache HTTP Server 解决方案的工作量要多得多。这与 用于提供静态内容的 Servlet 上发布的几个答案非常相似。具体来说,您可以使用或扩展 Apache Tomcat 的 默认Servlet。 (这里使用了一些 Tomcat 特定的类,但它们可以很容易地用通用的等效类替换。) http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html 看起来更接近你会寻找,它是完全通用的 - 同时仍然有一些额外的、重要的功能。

这些选项中的任何一个都是非常通用的,并不特定于任何特定的 servlet 容器。

If you're only serving files, I would consider fronting your servlet container with something like Apache HTTP Server, where you could simply use its various directives to provide a "virtual directory" pointing to an easily configured location.

Otherwise, you could write and configure a standard Java servlet that would do essentially the same thing - storing the actual path in a Java properties file that would be read by the servlet. But while this isn't a lot of work, it would be significantly more work that the above Apache HTTP Server solution. This would be very similar to several of the answers posted at Servlet for serving static content . Specifically, you could either use or extend upon Apache Tomcat's DefaultServlet. (There are some Tomcat-specific classes used in here, but they could be easily replaced with generic equivalents.) http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html looks even closer to what you'd be looking for, and it is completely generic - while still having some additional, significant features.

Either of these options would be very generic, and not specific to any particular servlet container.

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