Java使用Jetty實(shí)現(xiàn)靜態(tài)網(wǎng)站

在項(xiàng)目的pom.xml文件中假如依賴

<dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-server</artifactId>
       <version>9.4.19.v20190610</version>
</dependency>

配置從 https://mvnrepository.com/ 搜索獲得

運(yùn)行如下代碼

public class Main {
    public static void main( String[] arg) throws Exception {
        Server server = new Server(8111);
        ResourceHandler rh = new ResourceHandler();
        ContextHandler context = new ContextHandler();
        context.setContextPath("/files");
        File dir = new File("G:\\桌面文件");
        context.setBaseResource(Resource.newResource(dir));
        context.setHandler(rh);
        HandlerList handlerList = new HandlerList();
        handlerList.setHandlers(new Handler[]{context});
        server.setHandler(handlerList);
        server.start();
        server.join();
    }
}

server端口號(hào):8111
訪問的本地目錄:G:\桌面文件
url路徑:/files

在瀏覽器中訪問地址 http://localhost:8111/files/

未命名圖片.png
未命名圖片.png

總結(jié):這種方法其實(shí)就是用java的jetty庫制作一個(gè)靜態(tài)網(wǎng)站服務(wù)器,同樣的不用編程直接配置apache、nginx、iis等服務(wù)器亦可實(shí)現(xiàn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容