2011-11-29 54 views
7

我在Jetty服务器上有Java Web应用程序(Eclipse/OSGI)。我希望能够从web根目录以外的文件夹为我的Web应用程序提供静态文件。在我的Web应用程序中,我还不知道要提供的文件的文件名,因此我想在启动Web应用程序时将文件名(和/或路径)作为VM参数。例如:从Jetty服务器中的Web应用程序之外的文件夹提供文件

我有一个图像 - myImg.jpg - 我把它放在服务器文件系统的文件夹中,例如root/images/myImg.jpg。我想把它作为VM参数,例如“-DmyImg =/images/myImg.jpg /”,这样我就可以获取图像并将其显示在我的网页上。我怎样才能做到这一点?我可以在不创建新的Servlet的情况下执行此操作吗?

在此先感谢您的帮助!

+0

我试图使用在的jetty.xml配置文件ContextHandler中的,在我点了baseResource复制到文件系统上的实际文件夹,但当我尝试通过contextPath访问它时,资源为“null”。 – Farna

回答

11

解决了!

这是我加入到我的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> 
3

@Farna:在你的答案,我无法理解你是如何传递文件名的VM参数。这就是我所做的。

我在jetty webapps目录中创建了testparvez.xml文件。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd"> 

<Configure class="org.eclipse.jetty.server.handler.ContextHandler"> 
    <Set name="contextPath">/testparvez</Set> 
    <Set name="resourceBase"><SystemProperty name="mydir"/></Set> 
    <Set name="handler"> 
    <New class="org.eclipse.jetty.server.handler.ResourceHandler"> 
     <Set name="welcomeFiles"> 
     <Array type="String"> 
      <Item><SystemProperty name="myfile"/></Item> 
     </Array> 
     </Set> 
     <Set name="cacheControl">max-age=3600,public</Set> 
    </New> 
    </Set> 
</Configure> 

然后,我开始为码头

java -jar start.jar jetty.port=8082 -Dmydir=C:/test/javadoc/ -Dmyfile=index.html 

最后,我从网址访问http://localhost:8082/testparvez/

相关问题