希望在Jetty中使用多个静态目录。服务器运行时:
http://localhost:8282/A
http://localhost:8282/B
http://localhost:8282/C
以下失败:
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setWelcomeFiles(new String[]{"index.html"});
resource_handler.setResourceBase(HTML_SITE);
ResourceHandler resource_handler1 = new ResourceHandler();
resource_handler1.setWelcomeFiles(new String[]{"index.html"});
resource_handler1.setResourceBase(HTML_CLIENTZONE_SITE);
// deploy engine
WebAppContext webapp = new WebAppContext();
String dir = System.getProperty("user.dir");
webapp.setResourceBase(getWebAppPath());
webapp.setContextPath("/");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resource_handler,resource_handler1 ,webapp, new DefaultHandler()});
server.setHandler(handlers);
如何添加多个静态资源目录?
答案 0 :(得分:18)
从6.1.12开始,使用ResourceCollection支持WebAppContext的基础资源:
Server server = new Server(8282);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
ResourceCollection resources = new ResourceCollection(new String[] {
"project/webapp/folder",
"/root/static/folder/A",
"/root/static/folder/B",
});
context.setBaseResource(resources);
server.setHandler(context);
server.start();
要随后打开文件,请使用ServletContext(例如,WebAppContext),它可以是接口定义的一部分,例如:
/**
* Opens a file using the servlet context.
*/
public default InputStream open( ServletContext context, String filename ) {
String f = System.getProperty( "file.separator" ) + filename;
return context.getResourceAsStream( f );
}
如:
InputStream in = open( context, "filename.txt" );
如果它存在于某个给定目录中,则会打开filename.txt
。请注意,getResourceAsStream将返回null
,而不是抛出异常,因此检查它是个好主意:
public default InputStream validate( InputStream in, String filename )
throws FileNotFoundException {
if( in == null ) {
throw new FileNotFoundException( filename );
}
return in;
}
然后您可以按如下方式更新open
方法:
return validate( context.getResourceAsStream( filename ), filename );
答案 1 :(得分:2)
如果要将独立资源目录“挂载”到服务器命名空间中的任意URI,则使用具有独立DefaultServlet
配置的resourceBase
的多个实例是最佳选择。
以下代码段将从文件系统路径/A/*
提供URI X/V/A
,从文件系统路径/B/*
提供Q/Z/B
:
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(Integer.parseInt(port));
server.addConnector(connector);
ServletContextHandler servletHandler = new ServletContextHandler(null, "/", true, false);
// Configuration for serving /A/* from X/V/A
DefaultServlet aServlet = new DefaultServlet();
ServletHolder aHolder = new ServletHolder(aServlet);
aHolder.setInitParameter("resourceBase", "X/V/A");
aHolder.setInitParameter("pathInfoOnly", "true");
servletHandler.addServlet(aHolder, "/A/*");
// Configuration for serving /B/* from Q/Z/B
DefaultServlet bServlet = new DefaultServlet();
ServletHolder bHolder = new ServletHolder(bServlet);
bHolder.setInitParameter("resourceBase", "Q/Z/B");
bHolder.setInitParameter("pathInfoOnly", "true");
servletHandler.addServlet(bHolder, "/B/*");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { servletHandler });
server.setHandler(handlers);
server.start();
注意:正如您在上面的代码中看到的那样,提供/A/*
的目录名称与示例中的A
完全相同。两个名称 - 要服务的目录的文件系统名称和将内容映射到的URI都是完全独立的。