灰熊球衣中的静态内容

Ren*_*nie 3 java jersey grizzly

我想在 / 和 /index.html 中提供静态 html 页面

final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
Run Code Online (Sandbox Code Playgroud)

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

StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/static");
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
Run Code Online (Sandbox Code Playgroud)

我的问题是我应该把静态文件放在哪里

Mac*_*ach 6

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

有几个选项可以定位静态资源:

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

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

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

    StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
    
    Run Code Online (Sandbox Code Playgroud)