从Jetty中的Web应用程序外部文件夹提供文件 [英] Serve files from folder outside web application in Jetty
问题描述
我在Jetty服务器上有一个Java Web应用程序(Eclipse / OSGI)。我希望能够从Web根目录之外的文件夹向我的Web应用程序提供静态文件。在我的Web应用程序中,我还不知道我想要提供的文件的文件名,因此我想在启动Web应用程序时将文件名(和/或路径)作为VM参数。例如:
I have a Java web application (Eclipse/OSGI) on a Jetty server. I want to be able to serve static files to my web application from a folder outside of the web root. In my web application, I don't yet know the file name of the file I want to be served, so I want to take the filename (and/or path) as a VM parameter when I start my web application. For example:
我有一个图像 - myImg.jpg - 我放在服务器文件系统的一个文件夹中,例如root / images / myImg.jpg。我想将其作为VM参数,例如-DmyImg = / images / myImg.jpg /这样我就可以获取图像并将其显示在我的网页上。我怎么能做到这一点?我可以不创建新的Servlet吗?
I have an image - myImg.jpg - that I have put in a folder on the server file system, for example root/images/myImg.jpg. I want to take this as a VM parameter, e.g. "-DmyImg=/images/myImg.jpg/" so that I can get the image and display it on my web page. How can I accomplish this? Can I do this without creating a new Servlet?
提前感谢您的帮助!
推荐答案
解决了!
这是我添加到jetty.xml文件中的内容:
This is what I added to my jetty.xml file:
<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>
这篇关于从Jetty中的Web应用程序外部文件夹提供文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!