我在 Jetty 服务器上有一个 Java Web 应用程序(Eclipse/OSGI)。我希望能够从 Web 根目录之外的文件夹向我的 Web 应用程序提供静态文件。在我的 Web 应用程序中,我还不知道要提供的文件的文件名,因此我想在启动 Web 应用程序时将文件名(和/或路径)作为 VM 参数。例如:
我有一个图像 - myImg.jpg - 我已经将它放在服务器文件系统上的一个文件夹中,例如 root/images/myImg.jpg。我想把它作为一个 VM 参数,例如"-DmyImg=/images/myImg.jpg/"以便我可以获取图像并将其显示在我的网页上。我怎样才能做到这一点?我可以在不创建新 Servlet 的情况下执行此操作吗?
在此先感谢您的帮助!
最佳答案
解决了!
这是我添加到我的 jetty.xml 文件中的内容:
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/myContextPath</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="directoriesListed">false</Set>
<Set name="resourceBase">/actual/folder/on/file/system</Set>
</New>
</Set>
</New>
</Item>
[...other handlers...]
</Array>
</Set>
</New>
</Set>
关于web-services - 从 Jetty 中 Web 应用程序外部的文件夹提供文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8308608/