2015-08-19 169 views
2

我要服务于静态HTML页面/和/index.html静态内容

final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); 

BASE_URL = http://locahost:8080/api/

StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/static"); 
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/"); 

我的问题是我应该把静态文件

回答

3

在你的情况下,静态数据应该位于磁盘根目录下的/static文件夹中。

有几个选项,其中静态资源可以位于:

  • 你可以从你的文件系统通过绝对路径提供静态资源(例如/www/static/):

    StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/www/static/"); 
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/"); 
    
  • 或将您的资源嵌入到jar档案中(例如/www/static.jar):

    StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///www/static.jar")})); 
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/"); 
    
  • 或嵌入您的应用程序罐内的静态资源:

    StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader()); 
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");