有没有一种简单的方法可以在 web.xml 或其他部署描述符(jetty.xml 等)文件中映射目录?
例如,如果我有一个目录 /opt/files/ 是否可以通过访问http://localhost/some-mapping/来访问其文件和子目录?我觉得应该有一些简单的方法可以做到这一点,但我无法找出方法(通过谷歌、stackoverflow 等)。我发现的只是模仿文件服务器的 servlet,这不是我想要的。
作为参考,我在 AIX 机器上使用码头。
有没有一种简单的方法可以在 web.xml 或其他部署描述符(jetty.xml 等)文件中映射目录?
例如,如果我有一个目录 /opt/files/ 是否可以通过访问http://localhost/some-mapping/来访问其文件和子目录?我觉得应该有一些简单的方法可以做到这一点,但我无法找出方法(通过谷歌、stackoverflow 等)。我发现的只是模仿文件服务器的 servlet,这不是我想要的。
作为参考,我在 AIX 机器上使用码头。
不知道如何用 Jetty 来做,但在 Tomcat 中你可以添加一个新<Context>
的server.xml
:
<Context docBase="/opt/files" path="/files" />
这样它就可以通过http://example.com/files/...
. 看看 Jetty 是否存在类似的东西。
更新:谷歌搜索后,“普通 Java 代码”等价物将类似于:
WebAppContext files = new WebAppContext("/opt/files", "/files");
Server server = new Server(8080);
server.setHandler(files);
server.start();
现在还没有把它翻译成jetty.xml
味道。根据网上找到的文档和示例,我有点猜测,所以不要把我钉在上面:
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="webApp">/opt/files</Set>
<Set name="contextPath">/files</Set>
</Configure>
另一种可能是这样的:
<Configure class="org.mortbay.jetty.Server">
<Call name="addHandler">
<Arg>
<New class="org.mortbay.jetty.webapp.WebAppContext">
<Arg name="webApp">/opt/files</Arg>
<Arg name="contextPath">/files</Arg>
</New>
</Arg>
</Call>
</Configure>
经过一番摆弄之后,最好的方法(对于码头)是在上下文目录中部署一个描述符,如下所示......
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- Configuration of a custom context. -->
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Call class="org.eclipse.jetty.util.log.Log" name="debug">
<!-- The default message to show if our context breaks. -->
<Arg>Configure context.xml</Arg>
</Call>
<!--
The context path is the web location of the context in relation to the
server address.
-->
<Set name="contextPath">/context</Set>
<!--
The resource base is the server directory to use for fetching files.
-->
<Set name="resourceBase">/path/to/files/on/server</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="directoriesListed">true</Set>
<!-- For now we don't need any welcome files -->
<!--
<Set name="welcomeFiles"> <Array type="String">
<Item>index.html</Item> </Array> </Set>
-->
<!--
The cache time limit in seconds (ie max-age=3600 means that if the
document is older than 1 hour a fresh copy will be fetched).
-->
<Set name="cacheControl">max-age=3600,public</Set>
</New>
</Set>
</Configure>
我希望这对其他人有帮助!
我不知道您可以将其作为 URL 映射来执行,但是您的 web.xml 文件所在的同一目录中的文件和文件夹将以您描述的方式访问,尽管您无法控制您的映射网络应用程序。
这符合你的需要吗?