這裡提供兩種方法,但經過我的測試是 ResourceHandler 客戶端不能下載大檔案 (例如 > 5 MB) 會發生例外
而 WebAppContext 就沒這個問題,如果要使用大檔案建議使用 WebAppContext 來做會比較好

使用 WebAppContext 實作:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
 
public class FileServerXml {
 
    public static void main(String[] args) throws Exception {
 
        WebAppContext context = new WebAppContext();
        context.setResourceBase(".");
 
        Server server = new Server(8080);
        server.setHandler(context);
        server.start();
        server.join();
    }
}

使用 ResourceHandler 實作:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ResourceHandler;
 
public class FileServerXml {
 
    public static void main(String[] args) throws Exception {
 
        ResourceHandler rh = new ResourceHandler();
        rh.setResourceBase(".");
 
        Server server = new Server(8080);
        server.setHandler(rh);
        server.start();
        server.join();
    }
}
 


arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()