我想在/和/index.html
中提供静态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, "/");
我的问题是我应该把静态文件放在哪里
答案 0 :(得分:4)
在您的情况下,静态数据应位于磁盘根目录的/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, "/");
或在应用程序jar中嵌入静态资源:
StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");