7

Jetty サーバーに Java Web アプリケーション (Eclipse/OSGI) があります。Web ルート外のフォルダーから Web アプリケーションに静的ファイルを提供できるようにしたいと考えています。私の Web アプリケーションでは、提供したいファイルのファイル名がまだわからないので、Web アプリケーションを開始するときに VM パラメータとしてファイル名 (および/またはパス) を取得したいと考えています。例えば:

サーバー ファイル システムのフォルダー (root/images/myImg.jpg など) に保存した画像 (myImg.jpg) があります。画像を取得して自分の Web ページに表示できるように、これを VM パラメーター (たとえば "-DmyImg=/images/myImg.jpg/") として取得したいと考えています。どうすればこれを達成できますか? 新しいサーブレットを作成せずにこれを行うことはできますか?

助けてくれてありがとう!

4

2 に答える 2

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>
于 2011-11-30T09:08:44.153 に答える
3

@Farna:あなたの答えでは、ファイル名をVMパラメーターとして渡す方法を理解できません。これが私がしたことです。

testparvez.xmljetty ディレクトリにファイルを作成しましたwebapps

    <?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

そして最後にURLからアクセスしますhttp://localhost:8082/testparvez/

于 2012-12-30T19:04:43.703 に答える