2011-11-29 2 views
7

Jetty 서버에 Java 웹 응용 프로그램 (Eclipse/OSGI)이 있습니다. 웹 루트 외부의 폴더에서 내 웹 응용 프로그램에 정적 파일을 제공 할 수 있기를 원합니다. 웹 응용 프로그램에서 제공 할 파일 이름을 아직 모르기 때문에 웹 응용 프로그램을 시작할 때 파일 이름 (및/또는 경로)을 VM 매개 변수로 사용하려고합니다. 예를 들면 다음과 같습니다.Jetty의 웹 응용 프로그램 외부 폴더에서 파일 제공

저는 root/images/myImg.jpg와 같이 서버 파일 시스템에 폴더에 넣은 myImg.jpg 이미지가 있습니다. 이 매개 변수를 VM 매개 변수로 사용하려고합니다. "-DmyImg =/images/myImg.jpg /"그래서 이미지를 가져 와서 내 웹 페이지에 표시 할 수 있습니다. 이것을 어떻게 할 수 있습니까? 새 서블릿을 만들지 않고도이 작업을 수행 할 수 있습니까?

미리 도움을 청하십시오!

+0

, URL에서 액세스 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 매개 변수로 파일 이름을 전달하는 방법을 이해할 수 없습니다입니다. 이것이 내가 한 일이다.

나는디렉토리에 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 

로 부두를 시작 그리고 마지막으로 내가하는 I가 포인트 제가 jetty.xml의 설정 파일에 ContextHandler를 사용하기 위해 노력하고있어 http://localhost:8082/testparvez/