Jetty 9.2 文档ResourceHandler
提供了一个 Jetty Embedded 示例来使用 a而不是 servlet来提供静态文件:
// Create a basic Jetty server object that will listen on port 8080. Note that if you set this to port 0
// then a randomly available port will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(8080);
// Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
// a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
ResourceHandler resource_handler = new ResourceHandler();
// Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
// In this example it is the current directory but it can be configured to anything that the jvm has access to.
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
resource_handler.setResourceBase(".");
// Add the ResourceHandler to the server.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);
// Start things up! By using the server.join() the server thread will join with the current thread.
// See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
server.start();
server.join();
Jetty 使用 NIO(内存中文件映射),从而锁定 Windows 操作系统上的文件。这是一个已知问题,可以为 servlet 找到许多解决方法。
但是,由于此示例不依赖于 servlet,因此基于 webapp 参数(useFileMappedBuffer、maxCachedFiles)的相关答案不起作用。
为了防止内存中的文件映射,您需要添加以下配置行:
resource_handler.setMinMemoryMappedContentLength(-1);
注意:如 Javadoc 中所写(并由 nimrodm 注意到)the minimum size in bytes of a file resource that will be served using a memory mapped buffer, or -1 for no memory mapped buffers
:. 但是,我对 value 有相同的行为Integer.MAX_VALUE
。
设置此参数后,您的 Jetty 可以在 Windows 上提供静态文件并且您可以编辑它们。