2011-11-29 55 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/

相關問題